• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
Keine Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

Node.js sample application with Socket.IO


Commit MetaInfo

Revision00ab1e144e417bc42d78ae9d8415f1fbc7c19b64 (tree)
Zeit2012-10-25 00:32:15
Autorhylom <hylom@hylo...>
Commiterhylom

Log Message

fix basic socket connection protocol

Ändern Zusammenfassung

Diff

--- a/app.js
+++ b/app.js
@@ -37,3 +37,30 @@ var io = socketIO.listen(server);
3737 server.listen(app.get('port'), function(){
3838 console.log("Express server listening on port " + app.get('port'));
3939 });
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+
--- a/public/js/minichat.js
+++ b/public/js/minichat.js
@@ -1,5 +1,4 @@
11 // minichat.js
22
33 (function () {
4-
54 }).apply(this);
--- /dev/null
+++ b/public/js/room.js
@@ -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+
--- a/routes/index.js
+++ b/routes/index.js
@@ -9,13 +9,24 @@ exports.index = function(req, res){
99 };
1010
1111 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+
1223 var params = {
13- title: 'チャットルーム:' + req.params.name,
24+ title: 'チャットルーム:' + roomName,
1425 room: {
15- name: req.params.name,
16- password: ''
26+ name: roomName,
27+ password: hashedPassword
1728 },
18- user: {name: ''}
29+ user: {name: yourName}
1930 };
2031 res.render('room', params);
2132 };
--- a/views/room.jade
+++ b/views/room.jade
@@ -1,5 +1,13 @@
11 extends layout
22 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+ };
311
412 .navbar.navbar-inverse.navbar-fixed-top
513 .navbar-inner
@@ -15,16 +23,18 @@ block content
1523 .span4
1624 h3
1725 span#yourname #{user.name}
18- form(action='#')
26+ form
1927 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') メッセージを投稿
2230 .span8
2331 #messages.well
2432 .message
2533 p.postdate.pull-right 10/23 12:34
2634 p.author システムメッセージ:
2735 p.comment チャットルーム「hogehoge」が作成されました。HASH: #{room.password}
36+
37+
2838 hr
2939 footer
30- p minichat 0.0.1
\ No newline at end of file
40+ p minichat 0.0.1