[英]Javascript websocket - how to make sending data like in socket.io?
as title says, is there way to make sending data in websockets like in socket.io ? 就像标题所说,有没有办法像在socket.io中那样在websocket中发送数据?
I'm using uWS with nodejs as server. 我正在将uWS与nodejs用作服务器。
I mean something like this: 我的意思是这样的:
// client
var ws = new WebSocket('ws://localhost:3000/');
ws.send("event1",{data1:100,data2:500,etc:"so on"});
// server
//receive that message like so
ws.on("event1",function(data){
console.log(data.data1,data.data2,data.etc)
ws.send("sendbackThisMessage",data);
});
// client
//receive that message like so
ws.on("sendbackThisMessage",function(data){
console.log(data.data1,data.data2,data.etc)
});
const http = require('http');
const express = require('express');
const socketIO = require('socket.io');
const uws = require('uws');
const app = express();
const server = http.Server(app);
const io = socketIO(server);
io.ws = new uws.Server({ perMessageDeflate: false, noServer: true });
io.on('connection', (socket) => {
io.emit('data', 'hello!');
// or
socket.emit('data', 'hello')
});
server.listen(3000, () => console.log('Socket.IO Running...'));
node: v7.8.0 节点:v7.8.0
"dependencies": {
"express": "^4.15.3",
"socket.io": "^2.0.3",
"uws": "^8.14.0"
}
Basead on udgwebdev But the example of the site didn`t work for me. 基于udgwebdev的基础,但是该站点的示例对我不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.