Node.js sample application with Socket.IO
Revision | 00ab1e144e417bc42d78ae9d8415f1fbc7c19b64 (tree) |
---|---|
Zeit | 2012-10-25 00:32:15 |
Autor | hylom <hylom@hylo...> |
Commiter | hylom |
fix basic socket connection protocol
@@ -37,3 +37,30 @@ var io = socketIO.listen(server); | ||
37 | 37 | server.listen(app.get('port'), function(){ |
38 | 38 | console.log("Express server listening on port " + app.get('port')); |
39 | 39 | }); |
40 | + | |
41 | +var socketsOf = {}; | |
42 | + | |
43 | +io.sockets.on('connection', function (socket) { | |
44 | + socket.emit('connected', {}); | |
45 | + socket.on('regist request', function (data) { | |
46 | + console.log('regist client for ' + data.roomId); | |
47 | + if (socketsOf[data.roomId] !== undefined) { | |
48 | + socketsOf[data.roomId].push(socket); | |
49 | + } else { | |
50 | + socketsOf[data.roomId] = [socket]; | |
51 | + } | |
52 | + }); | |
53 | + | |
54 | + socket.on('say', function (data) { | |
55 | + console.log('receive message'); | |
56 | + socket.emit('say accept', {}); | |
57 | + if (socketsOf[data.roomId] !== undefined) { | |
58 | + var targets = socketsOf[data.roomId]; | |
59 | + for (var i = 0; i < targets.length; i++) { | |
60 | + targets[i].emit('push message', data); | |
61 | + } | |
62 | + } | |
63 | + }); | |
64 | + | |
65 | +}); | |
66 | + |
@@ -1,5 +1,4 @@ | ||
1 | 1 | // minichat.js |
2 | 2 | |
3 | 3 | (function () { |
4 | - | |
5 | 4 | }).apply(this); |
@@ -0,0 +1,34 @@ | ||
1 | +// room.js | |
2 | + | |
3 | + | |
4 | +(function () { | |
5 | + var socket; | |
6 | + | |
7 | + // ページロード時の処理 | |
8 | + $(document).ready(function () { | |
9 | + // ユーザー名、ルーム名、パスワードを送信 | |
10 | + socket = io.connect('http://localhost'); | |
11 | + // ページロード時の処理ここまで | |
12 | + socket.on('connected', function(data) { | |
13 | + socket.emit('regist request', {roomId: minichat.roomId}); | |
14 | + }); | |
15 | + socket.on('push message', function (data) { | |
16 | + alert(data.message); | |
17 | + }); | |
18 | + socket.on('say accept', function (data) { | |
19 | + $('#message').val(''); | |
20 | + }); | |
21 | + }); | |
22 | + | |
23 | + $('#post-message').live('click', function () { | |
24 | + socket.emit('say', { | |
25 | + name: minichat.userName, | |
26 | + message: $('#message').val(), | |
27 | + roomId: minichat.roomId | |
28 | + }); | |
29 | + }); | |
30 | + | |
31 | + | |
32 | + | |
33 | +}).apply(this); | |
34 | + |
@@ -9,13 +9,24 @@ exports.index = function(req, res){ | ||
9 | 9 | }; |
10 | 10 | |
11 | 11 | exports.echo = function(req, res) { |
12 | + var roomName = req.params.name; | |
13 | + var yourName = 'test'; | |
14 | + var password = 'minichattest'; | |
15 | + var hashedPassword = ''; | |
16 | + var shasum = crypto.createHash('sha512'); | |
17 | + | |
18 | + if (password !== '') { | |
19 | + shasum.update(password); | |
20 | + hashedPassword = shasum.digest('hex'); | |
21 | + } | |
22 | + | |
12 | 23 | var params = { |
13 | - title: 'チャットルーム:' + req.params.name, | |
24 | + title: 'チャットルーム:' + roomName, | |
14 | 25 | room: { |
15 | - name: req.params.name, | |
16 | - password: '' | |
26 | + name: roomName, | |
27 | + password: hashedPassword | |
17 | 28 | }, |
18 | - user: {name: ''} | |
29 | + user: {name: yourName} | |
19 | 30 | }; |
20 | 31 | res.render('room', params); |
21 | 32 | }; |
@@ -1,5 +1,13 @@ | ||
1 | 1 | extends layout |
2 | 2 | block content |
3 | + script(src='/js/room.js') | |
4 | + script | |
5 | + var minichat = { | |
6 | + roomName: '#{room.name}', | |
7 | + password: '#{room.password}', | |
8 | + userName: '#{user.name}', | |
9 | + roomId: '#{room.name}#{room.password}' | |
10 | + }; | |
3 | 11 | |
4 | 12 | .navbar.navbar-inverse.navbar-fixed-top |
5 | 13 | .navbar-inner |
@@ -15,16 +23,18 @@ block content | ||
15 | 23 | .span4 |
16 | 24 | h3 |
17 | 25 | span#yourname #{user.name} |
18 | - form(action='#') | |
26 | + form | |
19 | 27 | label |
20 | - textarea#new-message.span4(rows='5', placeholder='メッセージを入力...') | |
21 | - button(type='submit', class='btn') メッセージを投稿 | |
28 | + textarea#message.span4(rows='5', placeholder='メッセージを入力...') | |
29 | + button#post-message.btn.btn-primary(type='button') メッセージを投稿 | |
22 | 30 | .span8 |
23 | 31 | #messages.well |
24 | 32 | .message |
25 | 33 | p.postdate.pull-right 10/23 12:34 |
26 | 34 | p.author システムメッセージ: |
27 | 35 | p.comment チャットルーム「hogehoge」が作成されました。HASH: #{room.password} |
36 | + | |
37 | + | |
28 | 38 | hr |
29 | 39 | footer |
30 | - p minichat 0.0.1 | |
\ No newline at end of file | ||
40 | + p minichat 0.0.1 |