可以连接到Chat Websocket(Angular,socket.io)

发布于 2025-02-01 17:47:55 字数 1148 浏览 3 评论 0原文

试图连接到套接字时,我有一个主要问题。 我必须使用此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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

日记撕了你也走了 2025-02-08 17:47:55

当我看到您的代码时,您正在尝试从非安全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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文