node.js搭建簡單的websocket

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   檔案   ar   

1、首先在官網http://www.nodejs.org/下載NODE.JS

2、開啟命令列CMD,進入NODEJS\node_modules\的目錄,輸入npm install socket.io  安裝socket.io模組.別急著關掉此CMD視窗,後面有用

3、搭建服務端代碼server.js

 1 var http = require(‘http‘); 2 var io = require(‘socket.io‘); 3 var cisserver = http.createServer(function (request, response) { 4     response.writeHead(200, { ‘Content-Type‘: ‘text/html‘ }); 5     response.end(‘start!‘); 6  7 }).listen(8000); 8  9 var ccisSocket = io.listen(cisserver); //.set(‘log‘, 1);10 ccisSocket.on(‘connection‘, function (client) {11     client.on(‘message‘, function (data) {12         console.log(‘client message‘, data);13         client.emit(‘message‘, data);14         client.on(‘disconnect‘, function () { console.log(‘disconnect‘) })15     })16 })

4、切換之前NODEJS的命令列視窗輸入node server.js開啟監聽模式

5、用戶端代碼

    

 1   <!DOCTYPE html>  2     <html>  3     <head>  4         <script src=‘http://localhost:8000/socket.io/socket.io.js‘></script>  5     </head>  6     <body>  7         <script>  8             var HTML5WebSockets = {}; 9             HTML5WebSockets.socketio = {10                 yoursocket: null,11 12                 initialize: function () {13 14                     HTML5WebSockets.socketio.yoursocket = io.connect(‘http://localhost:8000‘);15 16                     HTML5WebSockets.socketio.yoursocket.on(‘connect‘, function () {17                         HTML5WebSockets.socketio.log(‘You are connected to Server<br />‘);18                         HTML5WebSockets.socketio.yoursocket.send("目前有值");19                     });20 21                     HTML5WebSockets.socketio.yoursocket.on(‘message‘, function (data) {22                         //alert(data);23                         HTML5WebSockets.socketio.log(‘Server Response:  ‘ + data + ‘<br />‘);24                     });25 26                     HTML5WebSockets.socketio.yoursocket.on(‘disconnect‘, function () {27                         HTML5WebSockets.socketio.log(‘You are disconnected from Server<br />‘);28                     });29 30                     document.querySelector(‘#sendMes‘).onclick = function () {31                         HTML5WebSockets.socketio.sendMessageToServer(document.getElementById("mes").value);32                         document.getElementById("mes").value = ‘‘;33                     };34 35                 },36                 sendMessageToServer: function (data) {37                     HTML5WebSockets.socketio.yoursocket.send(data);38                     HTML5WebSockets.socketio.log(‘Message to Server: ‘ + data + ‘<br />‘);39                 },40 41                 log: function (message) {42                     document.getElementById("log").innerHTML += message;43                 }44             }  45         </script> 46        47         <div id=‘log‘></div> 48         <input type=‘text‘ id=‘mes‘ /> 49         <button type=‘button‘ id=‘sendMes‘>Send</button> 50         <br />    51         <script> 52             HTML5WebSockets.socketio.initialize();  53         </script> 54     </body> 55     </html> 

7、運行HTML檔案,輸入值,等待服務端返回的結果

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.