Node.js sample application with Socket.IO
リビジョン | a71631aece2b041c01ff0217d702e2d3d9a313be (tree) |
---|---|
日時 | 2012-10-23 23:47:27 |
作者 | hylom <hylom@hylo...> |
コミッター | hylom |
add codes for socket.io
@@ -6,7 +6,8 @@ | ||
6 | 6 | var express = require('express') |
7 | 7 | , routes = require('./routes') |
8 | 8 | , http = require('http') |
9 | - , path = require('path'); | |
9 | + , path = require('path') | |
10 | + , socketIO = require('socket.io'); | |
10 | 11 | |
11 | 12 | var app = express(); |
12 | 13 |
@@ -28,7 +29,11 @@ app.configure('development', function(){ | ||
28 | 29 | |
29 | 30 | app.get('/', routes.index); |
30 | 31 | app.post('/room', routes.room); |
32 | +app.get('/echo/:name', routes.echo); | |
31 | 33 | |
32 | -http.createServer(app).listen(app.get('port'), function(){ | |
34 | +var server = http.createServer(app); | |
35 | +var io = socketIO.listen(server); | |
36 | + | |
37 | +server.listen(app.get('port'), function(){ | |
33 | 38 | console.log("Express server listening on port " + app.get('port')); |
34 | 39 | }); |
@@ -25,4 +25,8 @@ a { | ||
25 | 25 | |
26 | 26 | .message .author { |
27 | 27 | font-weight: bold; |
28 | -} | |
\ No newline at end of file | ||
28 | +} | |
29 | + | |
30 | +.message .comment { | |
31 | + word-wrap: break-word; | |
32 | +} |
@@ -1,22 +1,42 @@ | ||
1 | - | |
2 | 1 | /* |
3 | 2 | * GET home page. |
4 | 3 | */ |
5 | 4 | |
5 | +var crypto = require('crypto'); | |
6 | + | |
6 | 7 | exports.index = function(req, res){ |
7 | 8 | res.render('index', { title: 'minichat' }); |
8 | 9 | }; |
9 | 10 | |
11 | +exports.echo = function(req, res) { | |
12 | + var params = { | |
13 | + title: 'チャットルーム:' + req.params.name, | |
14 | + room: { | |
15 | + name: req.params.name, | |
16 | + password: '' | |
17 | + }, | |
18 | + user: {name: ''} | |
19 | + }; | |
20 | + res.render('room', params); | |
21 | +}; | |
22 | + | |
10 | 23 | exports.room = function(req, res){ |
11 | 24 | var roomName = req.body.roomName || ''; |
12 | 25 | var yourName = req.body.yourName || ''; |
13 | 26 | var password = req.body.password || ''; |
27 | + var hashedPassword = ''; | |
28 | + var shasum = crypto.createHash('sha512'); | |
29 | + | |
30 | + if (password !== '') { | |
31 | + shasum.update(password); | |
32 | + hashedPassword = shasum.digest('hex'); | |
33 | + } | |
14 | 34 | |
15 | 35 | var params = { |
16 | 36 | title: 'チャットルーム:' + roomName, |
17 | 37 | room: { |
18 | 38 | name: roomName, |
19 | - password: password | |
39 | + password: hashedPassword | |
20 | 40 | }, |
21 | 41 | user: {name: yourName} |
22 | 42 | }; |
@@ -7,5 +7,6 @@ html | ||
7 | 7 | script(src='/js/jquery-1.8.2.min.js') |
8 | 8 | script(src='/js/minichat.js') |
9 | 9 | script(src='/js/bootstrap.min.js') |
10 | + script(src='/socket.io/socket.io.js') | |
10 | 11 | body |
11 | - block content | |
\ No newline at end of file | ||
12 | + block content |
@@ -24,7 +24,7 @@ block content | ||
24 | 24 | .message |
25 | 25 | p.postdate.pull-right 10/23 12:34 |
26 | 26 | p.author システムメッセージ: |
27 | - p.comment チャットルーム「hogehoge」が作成されました。 | |
27 | + p.comment チャットルーム「hogehoge」が作成されました。HASH: #{room.password} | |
28 | 28 | hr |
29 | 29 | footer |
30 | 30 | p minichat 0.0.1 |
\ No newline at end of file |