我在亚马逊lightsail上有一个Nodejs服务器。我连接到它并为Nodejs做了必要的Apache配置。
我有2个Nodejs socket.io项目,我想从一个URL使用它们。
这些是Nextjs中的链接
const [socket1, setSocket1] = useState<Socket | null>(null);
const [socket2, setSocket2] = useState<Socket | null>(null);
useEffect(() => {
// first WebSocket
const socketOne = io("https://....../", { path: "/ws", transports: ["websocket"] });
setSocket1(socketOne);
// second WebSocket
const socketTwo = io("https://....../", { transports: ["websocket"] });
setSocket2(socketTwo);
// ComponentWillUnmount
return () => {
socketOne.disconnect();
socketTwo.disconnect();
};
}, []); // only run once
useEffect(() => {
if (socket1) {
socket1.on("connection", () => console.log("connected socket1"));
}
}, [socket1]);
useEffect(() => {
if (socket2) {
socket2.on("connection", (data: any) => {
console.log("Socket 2:", data);
});
}
}, [socket2]);
字符串
server1应用程序
const app = express();
const httpServer = createServer(app);
const io = new Server(httpServer, {transports: ["polling", "websocket",] , cors: { origin: ["https://clienturl.com/"] } });
型
server1应用程序
const app = express();
const httpServer = createServer(app);
const io = new Server(httpServer, {transports: ["polling", "websocket",] , cors: { origin: ["https://clienturl.com/"] } });
型
1条答案
按热度按时间qlzsbp2j1#
你可以使用命名空间。基本上,你将在相同的URL上启动socket.io服务器,但不同的路由(socket.io称之为命名空间)。在这里你可以阅读更多:https://socket.io/docs/v4/namespaces/这个特定的视频也展示了命名空间的实现:https://www.youtube.com/watch?v=ZKEqqIO7n-k。