当前位置: 首页 > 后端技术 > Node.js

Node.js+webSocket

时间:2023-04-03 18:46:24 Node.js

//引入WebSocket模块varws=require('nodejs-websocket')varPORT=3030varserver=ws.createServer(function(conn){console.log('newconnection')conn.on("text",function(str){console.log("接受数据"+str)conn.sendText("返回数据:"+str)})conn.on("close",function(code,reason){console.log("closeconnection")})conn.on("error",function(err){console.log(err)})}).listen(PORT)console.log('websocket监听端口:'+端口)github地址:https://github.com/Rossy11/node