Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #socket.io 통신과 websocket 통신 예제
- (websocket 통신은 브라우저 특성이 있어서 지원되지 않는 경우도 있어, socket.io 사용)
- 대신 websocket의 경우 web(http)를 기반으로 하기 때문에, 방화벽에서 걸리는 경우가 적다.
- #socket.io를 이용한 통신 (server)
- var io = require('socket.io').listen(8000);
- //var io = require('socket.io').listen(8000);
- io.sockets.on('connection', function (socket) {
- socket.emit('example message 1', { hello: 'world 1' });
- socket.on('example message 2', function (data) {
- console.log(data);
- });
- });
- -------------------------------------
- #socket.io를 이용한 통신 (client - web)
- -> 웹브라우저에서 통신
- <!DOCTYPE HTML>
- <html>
- <head>
- <title>Example Socket.IO</title>
- </head>
- <body>
- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
- <script src="http://192.168.0.105:8000/socket.io/socket.io.js"></script>
- <script type="text/javascript">
- var socket = io.connect('http://192.168.0.105:8000');
- socket.on('example message 1', function (data) {
- $('#messages').append($('<li>').text('Received message: ' + data.hello));
- });
- socket.emit('example message 2', { hello: 'world 2' });
- </script>
- Messages: <ul id="messages"></ul>
- </body>
- </html>
- ======================================
- #websocket (server, run node websocket.js)
- var WebSocketServer = require('websocket').server;
- var http = require('http');
- var server = http.createServer(function (req, res) {
- console.log('Received request for ' + req.url);
- res.writeHead(404);
- res.end();
- });
- server.listen(8000, function () {
- console.log('Server is listening on port 8000');
- });
- wsServer = new WebSocketServer({
- httpServer: server,
- autoAcceptConnections: false
- });
- wsServer.on('request', function (request) {
- var connection = request.accept('example-echo', request.origin);
- connection.on('message', function (message) {
- if (message.type === 'utf8') {
- console.log('Received message: ' + message.utf8Data);
- connection.sendUTF(message.utf8Data);
- }
- else if (message.type === 'binary') {
- connection.sendBytes(message.binaryData);
- }
- connection.on('close', function (reasonCode, description) {
- console.log('Peer ' + connection.remoteAddress + ' disconnected.');
- });
- });
- });
- -----------------------------------------
- #websocket (client, web)
- <!DOCTYPE HTML>
- <html>
- <head>
- <title>Example WebSocket</title>
- </head>
- <body>
- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
- <script type="text/javascript">
- if ('WebSocket' in window) {
- var ws = new WebSocket('ws://192.168.0.105:8000', 'example-echo');
- ws.onopen = function () {
- $('#status').text('connected');
- for (var i = 0; i < 10; i++) {
- ws.send('Hello ' + i);
- }
- };
- ws.onmessage = function (evt) {
- $('#messages').append($('<li>').text('Received message: ' + evt.data));
- };
- ws.onclose = function () {
- $('#status').text('connection is closed');
- };
- }
- else
- $('#status').text('WebSocket not supported.');
- </script>
- Status: <span id="status"></span><br /><br />
- Messages: <ul id="messages"></ul>
- </body>
- </html>
- ============================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement