跳到主要内容

语法

2023年12月27日
柏拉文
越努力,越幸运

一、WS Koa


const WS = require("ws");
const Koa = require("koa");
const HTTP = require("http");

const PORT = 3000;
const app = new Koa();
const clients = new Set();
const server = HTTP.createServer(app.callback());
const wss = new WS.Server({ server });

wss.on("connection", (ws) => {
clients.add(ws);

ws.on("message", (message) => {
console.log("Websocket message: ", message)
});

ws.on("close", () => {
clients.delete(ws);
});
});

server.listen(PORT, () => {
console.log("Server listening on port " + PORT);
});

二、WS Express