# nodejs-websocket-example **Repository Path**: nealwang2021/nodejs-websocket-example ## Basic Information - **Project Name**: nodejs-websocket-example - **Description**: nodejs验证代码 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2022-12-05 - **Last Updated**: 2022-12-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # nodejs-websocket-example it is an example of implementing websocket(ws) on both server and client side ## Install ``` $ npm install --save ws express or $ git clone git@github.com:wahengchang/nodejs-websocket-example.git ``` ## Unstanding ws `ws` is a WebSocket client and server implementation, fast, and easy to use ( [R]ead More](https://stackoverflow.com/questions/16392260/which-websocket-library-to-use-with-node-js) ). #### client `websocket client` is a browser supported object. There are 3 basic must know fucntions: - `ws.onopen` : emmited when connected - `ws.send` : sending a send event to websocket server - `ws.onmessage` : event emmited when receiving message ([Read More](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications)) ```js ``` #### server server code is simple. ```js var WebSocketServer = require('ws').Server, wss = new WebSocketServer({port: 40510}) wss.on('connection', function (ws) { ws.on('message', function (message) { console.log('received: %s', message) }) setInterval( () => ws.send(`${new Date()}`), 1000 ) }) ``` ## Run Run server ``` $ npm start ``` Open browser ``` http://localhost:3000/ ``` ![image](https://user-images.githubusercontent.com/5538753/32210952-8d294d32-bdcd-11e7-9d14-b924fe52aacb.png) ## Reference - [https://github.com/wahengchang/nodejs-websocket-example](https://github.com/wahengchang/nodejs-websocket-example) - [https://github.com/websockets/ws](https://github.com/websockets/ws) - [https://websockets.github.io/ws/](https://websockets.github.io/ws/)