创建服务端
1、准备
添加包配置时,同时添加 ws 包(package.json)
{
"name": "demo",
"description": "socket.d for node.js demo",
"author": "noear",
"dependencies": {
"@noear/socket.d": "2.5.12"
}
}
安装包
npm i
2、使用独立端口
const {SocketD} = require('@noear/socket.d');
//创建 socket.d server
const server = SocketD.createServer("sd:ws")
.config(c => c.port(8602))
.start();
3、或者,使用 Http-Server 端口
const {SocketD} = require('@noear/socket.d');
const http = require('http');
//创建 http server
const httpServer = http.createServer();
httpServer.listen(8602);
//创建 socket.d server
const server = SocketD.createServer("sd:ws")
.config(c => c.httpServer(httpServer))
.start();