Node.js+webSocket


// 引入WebSocket模块
var ws = require('nodejs-websocket')
var PORT = 3030

var server = ws.createServer(function(conn){
    console.log('新连接')
    conn.on("text",function(str){
        console.log("接受数据"+str)
        conn.sendText("返回数据:"+str)
    })
    conn.on("close",function(code,reason){
        console.log("关闭连接")
    })
    conn.on("error",function(err){
        console.log(err)
    })
}).listen(PORT)

console.log('websocket 监听端口: ' + PORT)

github地址:https://github.com/Rossy11/node

来源:https://segmentfault.com/a/1190000016850510

猜你喜欢

转载自www.cnblogs.com/datiangou/p/10164312.html