正在回答 回答被采纳积分+1
1回答
Brian
2021-01-12 09:43:45
你本地有没有websocket的服务端?运行在3000端口上!?
websocket分客户端与服务端,与我们的接口服务很类似,koa是提供接口服务的,前端使用axios进行访问。
ws这个库有点特殊,即可以作服务端:
const WebSocket = require('ws');const wss = new WebSocket.Server({
port: 8080,
perMessageDeflate: {
zlibDeflateOptions: {
// See zlib defaults.
chunkSize: 1024,
memLevel: 7,
level: 3
},
zlibInflateOptions: {
chunkSize: 10 * 1024
},
// Other options settable:
clientNoContextTakeover: true, // Defaults to negotiated value.
serverNoContextTakeover: true, // Defaults to negotiated value.
serverMaxWindowBits: 10, // Defaults to negotiated value.
// Below options specified as default values.
concurrencyLimit: 10, // Limits zlib concurrency for perf.
threshold: 1024 // Size (in bytes) below which messages
// should not be compressed.
}});
也可以作客户端:
const WebSocket = require('ws');
const ws = new WebSocket('ws://www.host.com/path');
ws.on('open', function open() {
ws.send('something');}
);
ws.on('message', function incoming(data) {
console.log(data);
});
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星