websocket

ws

新建目录
npm install nodejs-websocket

当前目录新建 wsServer.js 添加

var ws = require("nodejs-websocket")
var port = 3000;
// Scream server example: "hi" -> "HI!!!"
var server = ws.createServer(function (conn) {
    console.log("New connection")
    conn.on("text", function (str) {
        console.log("Received " + str)
        conn.sendText(str.toUpperCase() + "!!!")
    })
    conn.on("close", function (code, reason) {
        console.log("Connection closed")
    })
    conn.on("error", function (err) {
        console.log("handdle error");
        console.log(err);
    })
}).listen(port);
console.log("websocket server listening on port " + port);

然后 node wsServer.js
新建HTML添加





    
    websocket



    

echo test

你可能感兴趣的:(websocket)