可以连接到Chat Websocket(Angular,socket.io)
试图连接到套接字时,我有一个主要问题。 我必须使用此url wss://socketio-chat-h9jt.herokuapp.com
当我在本地启动服务器并正确设置CORS时:
const io = require('socket.io')(server, {
cors: {
origin: "http://localhost:4200",
methods: ["GET", "POST"]
}
});
它的工作原理很好。不幸的是,我必须使用该特定网址。当我在服务中更改它时:
socket = io('wss://socketio-chat-h9jt.herokuapp.com');
它不再起作用了。我有组件可以稳定。创建时log连接状态。服务器在本地运行时,我会收到连接的消息,当我使用URL时,什么也不会发生... 我什至有错误的侦听器,当我使用wss://socketio-chat-h9jt.herokuapp.com
时,它会打印此错误:
Error: server error
at Socket.onPacket (socket.js:317:1)
at Polling.Emitter.emit (index.mjs:136:1)
at Polling.onPacket (transport.js:105:1)
at callback (polling.js:118:18)
at Array.forEach (<anonymous>)
at Polling.onData (polling.js:121:53)
at Request.Emitter.emit (index.mjs:136:1)
at Request.onLoad (polling.js:365:18)
at XMLHttpRequest.xhr.onreadystatechange [as __zone_symbol__ON_PROPERTYreadystatechange] (polling.js:301:26)
at XMLHttpRequest.wrapFn (zone.js:766:1)
有人可以帮忙吗?我正在使用socket.io-client(最新版本)的Angular 13。
I have a major problem when trying to connect to socket.
I have to use this url wss://socketio-chat-h9jt.herokuapp.com
When I am starting server locally and I setup CORS correctly:
const io = require('socket.io')(server, {
cors: {
origin: "http://localhost:4200",
methods: ["GET", "POST"]
}
});
it works rly good. Unfortunately I have to use that specific URL. When I change it in my service:
socket = io('wss://socketio-chat-h9jt.herokuapp.com');
it is not working anymore. I have component that should console.log connection status when created. When server is running locally i get CONNECTED message, when I use URL nothing happens...
I even have error listener, when I'm using wss://socketio-chat-h9jt.herokuapp.com
it prints this error:
Error: server error
at Socket.onPacket (socket.js:317:1)
at Polling.Emitter.emit (index.mjs:136:1)
at Polling.onPacket (transport.js:105:1)
at callback (polling.js:118:18)
at Array.forEach (<anonymous>)
at Polling.onData (polling.js:121:53)
at Request.Emitter.emit (index.mjs:136:1)
at Request.onLoad (polling.js:365:18)
at XMLHttpRequest.xhr.onreadystatechange [as __zone_symbol__ON_PROPERTYreadystatechange] (polling.js:301:26)
at XMLHttpRequest.wrapFn (zone.js:766:1)
Can someone help? I'm using Angular 13 with socket.io-client (latest version).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当我看到您的代码时,您正在尝试从非安全URL连接套接字,并且您已经安全地创建了套接字
,因此您已经使用
-SSL
您可以参考文档
< a href =“ https://betterprogramming.pub/how-to-serve-your-angular-angular-application-ober-https-usis-using-ser-ng-serve-ng-serve-240e2c2e0a5d” rel =“ nofollow noreferrer”> link 该指南您如何通过
https //
安全地运行Angular项目,希望这可以帮助您前进
As I saw your code you are trying to connect your socket from un-secure URL and you have created the socket securely
so, you have served your angular application using
--ssl
You can refer to the docs Link that guide you on how to run your angular project securely over
HTTPS//
I hope this helps you to move forward