[js]socket.io最簡單的例子
阿新 • • 發佈:2019-01-23
很久沒寫博,最近用到了socket.io,在網上找了挺多例子都不太好懂,於是自己查了文件寫了個示例,僅當筆記記錄到CSDN。
伺服器示例程式碼:
客戶端示例程式碼:'use strict'; const socket_io = require('socket.io'); const http = require('http'); const server = http.createServer(); const io = socket_io(server, { path : '/', // pingTimeout : 60000, // pingInterval : 25000, // 預設每隔 25 秒向客戶端發一次 ping 訊息 }); var sockets = {}; process.on('SIGINT', () => { // 不斷開與客戶端的連線,則客戶端預設會每隔 1~5 秒自動發起一次對伺服器的重連 // for (let id in sockets) // { // sockets[id].removeAllListeners(); // sockets[id].disconnect(true); // } // 關閉 socket.io 伺服器,不再接受客戶端連線 server.close(); }); // 客戶端回撥 var cli_cb = function (...args) { console.log('client callback:', ...args); }; // 廣播訊息到除 socket 外的客戶端 var broadcast = function (socket, event, ...args) { socket.broadcast.emit(event, ...args); }; io.on('connect', (socket) => { console.log('connection:', socket.id); sockets[socket.id] = socket; // 給剛建立連線的 server 客戶端傳送訊息 socket.emit('cli_event', socket.id, Math.random(), cli_cb); // 廣播到除客戶端 socket 自身外已連線 server 的所有客戶端 broadcast(socket, 'cli_broadcast', socket.id, { hello : socket.id, world : { lilei : undefined } }); // 廣播到所有連線 server 的客戶端 io.emit('cli_whole', socket.id, { hello : 'wolrd' }); // 處理客戶端發來的訊息 socket.on('srv_event', (id, param, fn_ack) => { console.log('%s srv_event', socket.id, id, param); // 向客戶端回覆訊息 if (fn_ack) { fn_ack(socket.id, { srv_rpc : Math.random() }); } }); socket.on('disconnect', (reason) => { broadcast(socket, 'cli_closed', socket.id); console.log('%s disconnected: %s', socket.id, reason); socket.removeAllListeners(); socket.disconnect(true); delete sockets[socket.id]; }) .on('error', (err) => { console.log('error: %s', err.stack); }); }); server.listen(12345);
'use strict' const socket_io_client = require('socket.io-client'); const socket = socket_io_client('http://127.0.0.1:12345'); // const socket = socket_io_client('http://182.254.229.133:12345'); process.on('SIGINT', () => { socket.disconnect(); }); // 伺服器回覆的訊息 var srv_cb = function (...args) { console.log('server callback:', ...args); }; socket.on('connect', () => { console.log('%s connected', socket.id); }) .on('disconnect', (reason) => { console.log('Disconnected: %s', reason); socket.disconnect(); }) .on('reconnect', (attempt) => // 重連成功會觸發此事件 { // attempt 為嘗試重連的次數 console.log('reconnect', attempt); }) .on('reconnect_attempt', () => { console.log('reconnect_attempt'); }) .on('reconnecting', (attempt) => // 正在重連時會觸發此事件 { console.log('reconnecting', attempt); }) .on('reconnect_error', (err) => { console.log('reconnect_error', err); }) .on('reconnect_failed', () => { console.log('reconnect_failed'); }) .on('ping', () => { console.log('ping received'); }) .on('pong', (latency) => { // latency 為從傳送 ping 後到收到伺服器迴應 pong 中間間隔的毫秒數 console.log('pong %d ms', latency); }) .on('error', (err) => { console.log('error: %s', err.stack); }) // 伺服器發來一般訊息 .on('cli_event', (id, param, fn_ack) => { console.log('%s cli_event', id, param); socket.emit('srv_event', socket.id, param, srv_cb); // 回覆伺服器訊息 if (fn_ack) { fn_ack(socket.id, { client : Math.random() }); } }) // 伺服器發來其他客戶端訊息 .on('cli_broadcast', (id, ...args) => { console.log('%s cli_broadcast', id, ...args); }) // 伺服器發來廣播訊息 .on('cli_whole', (id, ...args) => { console.log('%s cli_whole', id, ...args); }) // 伺服器要求客戶端斷開連線 .on('cli_closed', (id) => { console.log('cli_closed', id); socket.disconnect(); });
node v6.11.2 + socket.io v2.0.4 測試成功