Websocket 客户端覆盖彼此的连接
Websocket clients override connections of each other
我正在尝试使用 Nodejs Express 服务器和 LitComponent-Client 实现服务器-客户端示例。当 运行 宁一个 1:1 配置时,服务器和客户端之间的通信工作完美。当启动第二个客户端时,数据传输停止工作,只有新客户端接收数据。
localhost:3001
上的所有客户端 运行 - localhost:3000
上的服务器 运行。
如何连续向多个客户端发送数据?
客户端
import short from 'short-uuid';
import { io } from 'socket.io-client';
...
constructor() {
super();
//Generate app id
this.app_id = short.generate();
console.log('# App-ID: ' + this.app_id);
this.socket = io('ws://localhost:3000', { forceNew: true });
this.socket.on('connect', () => {
this.socket.emit('log', 'App-ID: ' + this.app_id);
});
this.socket.on('data', message => {
this.receiveData(message);
});
this.socket.on('disconnect', function () {
console.log('# Socket disconnected');
});
}
...
服务器
var app = require("express")();
const express = require("express");
var http = require("http").createServer(app);
var io = require("socket.io")(http, {
cors: {
origin: "http://localhost:3001",
methods: ["GET", "POST"],
},
});
let connectCounter = 0;
let sock;
io.on("connect", (socket) => {
sock = socket;
console.log("# " + socket.id + " | ■ App connected");
connectCounter++;
console.log("# New connection - now have " + connectCounter);
socket.on("disconnect", () => {
connectCounter--;
console.log("# Lost connection - now have " + connectCounter);
});
socket.on("log", (message) => {
console.log("# " + socket.id + " | " + message);
});
});
function sendData(msg) {
sock.emit("data", msg);
}
...
http.listen(3000, () => {
console.log("# HTTP: listening on *:3000");
});
在服务器端你有:
let sock;
io.on("connect", (socket) => {
sock = socket;
...
});
function sendData(msg) {
sock.emit("data", msg);
}
你能看出这里有问题吗?每次新用户连接时,您都将他设置为全局 sock
然后您只发送到该套接字。您需要的是将所有套接字保存在某个集合中,如下所示:
let sockets = {};
io.on("connect", (socket) => {
sockets[socket.id] = socket;
...
socket.on("disconnect", () => {
...
delete sockets[socket.id];
});
});
function sendData(msg) {
for (var socket_id in sockets) {
sockets[socket_id].emit("data", msg);
}
}
AFAIK socket.io 已经为你做到了,但我不记得具体的属性(自从我处理 socket.io 以来已经有一段时间了),所以你必须阅读 api.
我正在尝试使用 Nodejs Express 服务器和 LitComponent-Client 实现服务器-客户端示例。当 运行 宁一个 1:1 配置时,服务器和客户端之间的通信工作完美。当启动第二个客户端时,数据传输停止工作,只有新客户端接收数据。
localhost:3001
上的所有客户端 运行 - localhost:3000
上的服务器 运行。
如何连续向多个客户端发送数据?
客户端
import short from 'short-uuid';
import { io } from 'socket.io-client';
...
constructor() {
super();
//Generate app id
this.app_id = short.generate();
console.log('# App-ID: ' + this.app_id);
this.socket = io('ws://localhost:3000', { forceNew: true });
this.socket.on('connect', () => {
this.socket.emit('log', 'App-ID: ' + this.app_id);
});
this.socket.on('data', message => {
this.receiveData(message);
});
this.socket.on('disconnect', function () {
console.log('# Socket disconnected');
});
}
...
服务器
var app = require("express")();
const express = require("express");
var http = require("http").createServer(app);
var io = require("socket.io")(http, {
cors: {
origin: "http://localhost:3001",
methods: ["GET", "POST"],
},
});
let connectCounter = 0;
let sock;
io.on("connect", (socket) => {
sock = socket;
console.log("# " + socket.id + " | ■ App connected");
connectCounter++;
console.log("# New connection - now have " + connectCounter);
socket.on("disconnect", () => {
connectCounter--;
console.log("# Lost connection - now have " + connectCounter);
});
socket.on("log", (message) => {
console.log("# " + socket.id + " | " + message);
});
});
function sendData(msg) {
sock.emit("data", msg);
}
...
http.listen(3000, () => {
console.log("# HTTP: listening on *:3000");
});
在服务器端你有:
let sock;
io.on("connect", (socket) => {
sock = socket;
...
});
function sendData(msg) {
sock.emit("data", msg);
}
你能看出这里有问题吗?每次新用户连接时,您都将他设置为全局 sock
然后您只发送到该套接字。您需要的是将所有套接字保存在某个集合中,如下所示:
let sockets = {};
io.on("connect", (socket) => {
sockets[socket.id] = socket;
...
socket.on("disconnect", () => {
...
delete sockets[socket.id];
});
});
function sendData(msg) {
for (var socket_id in sockets) {
sockets[socket_id].emit("data", msg);
}
}
AFAIK socket.io 已经为你做到了,但我不记得具体的属性(自从我处理 socket.io 以来已经有一段时间了),所以你必须阅读 api.