当服务器具有基本URL时,为什么我的Web套接字呼叫失败?
我面临一个奇怪的问题。我有一个明确的后端服务器。当我没有用于后端的基本URL时,WS调用正常工作:
但是,如果我添加一个基本URL(例如,添加/api/作为URL的一部分),,我的连接失败:
这是我后端代码的一部分:
const easyrtc = require("open-easyrtc"); // EasyRTC external module
// Start Express http server
const webServer = http.createServer(app);
const socketIo = require("socket.io")(webServer, {path: '/api/'}); // web socket external module
// Start Socket.io so it attaches itself to Express server
const socketServer = socketIo.listen(webServer, { "log level": 1 });
easyrtc.listen(app, socketServer, null, (err, rtcRef) => {
console.log("Initiated");
rtcRef.events.on(
"roomCreate",
(appObj, creatorConnectionObj, roomName, roomOptions, callback) => {
console.log("roomCreate fired! Trying to create: " + roomName);
appObj.events.defaultListeners.roomCreate(
appObj,
creatorConnectionObj,
roomName,
roomOptions,
callback
);
}
);
});
// Listen on port
webServer.listen(port, () => {
console.log("listening");
});
为什么会发生这种情况以及如何修复它?我已经茫然了一个星期。
I am facing a strange problem. I have a express backend server. When I do not have a base URL for my backend, the ws calls are working fine:
But, if I add a base URL ( for example, adding /api/ as part of the url ), my connections fail:
This the part of my backend code:
const easyrtc = require("open-easyrtc"); // EasyRTC external module
// Start Express http server
const webServer = http.createServer(app);
const socketIo = require("socket.io")(webServer, {path: '/api/'}); // web socket external module
// Start Socket.io so it attaches itself to Express server
const socketServer = socketIo.listen(webServer, { "log level": 1 });
easyrtc.listen(app, socketServer, null, (err, rtcRef) => {
console.log("Initiated");
rtcRef.events.on(
"roomCreate",
(appObj, creatorConnectionObj, roomName, roomOptions, callback) => {
console.log("roomCreate fired! Trying to create: " + roomName);
appObj.events.defaultListeners.roomCreate(
appObj,
creatorConnectionObj,
roomName,
roomOptions,
callback
);
}
);
});
// Listen on port
webServer.listen(port, () => {
console.log("listening");
});
Why does this happen and how can I fix it? I have been at a loss on this for a week now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
”
/socket.io/
是路径
选项的默认值。如果您使用{路径:'/api/'}
选项,则应致电ws:// localHost:3000/api/socket.io/?eio = 3 .. 。
https://socket.io/docs/v4/server-options/
The
/socket.io/
is default value forpath
option. If you use{path: '/api/'}
option, then you should callinstead of
ws://localhost:3000/api/socket.io/?EIO=3...