Advertisement
zmnkh

Untitled

Oct 17th, 2017
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. #socket.io 통신과 websocket 통신 예제
  2. (websocket 통신은 브라우저 특성이 있어서 지원되지 않는 경우도 있어, socket.io 사용)
  3. 대신 websocket의 경우 web(http)를 기반으로 하기 때문에, 방화벽에서 걸리는 경우가 적다.
  4.  
  5.  
  6. #socket.io를 이용한 통신 (server)
  7.  
  8. var io = require('socket.io').listen(8000);
  9. //var io = require('socket.io').listen(8000);
  10.  
  11. io.sockets.on('connection', function (socket) {
  12. socket.emit('example message 1', { hello: 'world 1' });
  13.  
  14. socket.on('example message 2', function (data) {
  15. console.log(data);
  16. });
  17. });
  18.  
  19.  
  20. -------------------------------------
  21.  
  22. #socket.io를 이용한 통신 (client - web)
  23.  
  24. -> 웹브라우저에서 통신
  25.  
  26. <!DOCTYPE HTML>
  27. <html>
  28. <head>
  29. <title>Example Socket.IO</title>
  30. </head>
  31. <body>
  32.  
  33. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  34. <script src="http://192.168.0.105:8000/socket.io/socket.io.js"></script>
  35. <script type="text/javascript">
  36. var socket = io.connect('http://192.168.0.105:8000');
  37. socket.on('example message 1', function (data) {
  38. $('#messages').append($('<li>').text('Received message: ' + data.hello));
  39. });
  40.  
  41. socket.emit('example message 2', { hello: 'world 2' });
  42. </script>
  43. Messages: <ul id="messages"></ul>
  44. </body>
  45. </html>
  46.  
  47. ======================================
  48.  
  49. #websocket (server, run node websocket.js)
  50.  
  51. var WebSocketServer = require('websocket').server;
  52. var http = require('http');
  53.  
  54. var server = http.createServer(function (req, res) {
  55. console.log('Received request for ' + req.url);
  56. res.writeHead(404);
  57. res.end();
  58. });
  59.  
  60. server.listen(8000, function () {
  61. console.log('Server is listening on port 8000');
  62. });
  63.  
  64. wsServer = new WebSocketServer({
  65. httpServer: server,
  66. autoAcceptConnections: false
  67. });
  68.  
  69. wsServer.on('request', function (request) {
  70. var connection = request.accept('example-echo', request.origin);
  71. connection.on('message', function (message) {
  72. if (message.type === 'utf8') {
  73. console.log('Received message: ' + message.utf8Data);
  74. connection.sendUTF(message.utf8Data);
  75. }
  76. else if (message.type === 'binary') {
  77. connection.sendBytes(message.binaryData);
  78. }
  79.  
  80. connection.on('close', function (reasonCode, description) {
  81. console.log('Peer ' + connection.remoteAddress + ' disconnected.');
  82.  
  83. });
  84. });
  85. });
  86.  
  87. -----------------------------------------
  88.  
  89. #websocket (client, web)
  90. <!DOCTYPE HTML>
  91. <html>
  92. <head>
  93. <title>Example WebSocket</title>
  94. </head>
  95. <body>
  96.  
  97. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  98. <script type="text/javascript">
  99. if ('WebSocket' in window) {
  100. var ws = new WebSocket('ws://192.168.0.105:8000', 'example-echo');
  101.  
  102. ws.onopen = function () {
  103. $('#status').text('connected');
  104.  
  105. for (var i = 0; i < 10; i++) {
  106. ws.send('Hello ' + i);
  107. }
  108. };
  109.  
  110. ws.onmessage = function (evt) {
  111. $('#messages').append($('<li>').text('Received message: ' + evt.data));
  112. };
  113.  
  114. ws.onclose = function () {
  115. $('#status').text('connection is closed');
  116. };
  117. }
  118. else
  119. $('#status').text('WebSocket not supported.');
  120. </script>
  121.  
  122. Status: <span id="status"></span><br /><br />
  123. Messages: <ul id="messages"></ul>
  124. </body>
  125. </html>
  126.  
  127. ============================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement