Last active
August 29, 2015 14:21
-
-
Save junishitsuka/eaa71f0c7774c7bdb157 to your computer and use it in GitHub Desktop.
Node.jsからSocket.IOを使うための事前知識 ref: http://qiita.com/junishitsuka/items/2c66d501f29bff3830f7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| npm install socket.io |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var socket = io.connect(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| socket.on('greeting', function(data, fn) { | |
| var answer = confirm(data.message); | |
| fn(answer); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html> | |
| <head> | |
| <title>Socket.IO Test</title> | |
| <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script> | |
| <script src="/socket.io/socket.io.js"></script> | |
| <script> | |
| $(function() { | |
| var socket = io.connect(); | |
| socket.on('greeting', function(data, fn) { | |
| var answer = confirm(data.message); | |
| fn(answer); | |
| }); | |
| }); | |
| </script> | |
| </head> | |
| <body> | |
| <h1>Socket.IO Test</h1> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 上記のserver.jsに以下の内容を追加 | |
| io.sockets.on('connection', function(socket) { | |
| socket.emit('greeting', {message: 'hello'}, function (data) { | |
| console.log('result: ' + data); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment