角单页的应用程序和socketio多个套接字 [英] Angular single page app and socketio multiple sockets

查看:154
本文介绍了角单页的应用程序和socketio多个套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还没有找到一个解决方案或者对这个话题的任何信息。基本上我发展与瓶中,socketio和angularjs聊天应用程序。我创建了angularjs单页的应用程序,所以当我点击例如注册按钮页面映入眼帘,即使它实际上不是路由到一个新的一页。问题是,当我点击注册按钮或切换视图另一个套接字连接被创建。所以,当我回到聊天应用视图发送一条消息发送3消息,因为3个插座被创造。一开始,人们当我去注册视图,当我回到聊天应用程序创建视图的第三个套接字连接。所以我对这种行为的几个问题:

在非SPA将这种行为还是发生了?

是否在非SPA切换页面时的插座断开连接?

使用我的问题,我可以看到,在一个不断变化的SPA意见创建另一个插槽,但它不会删除插座。如何避免有多个socket连接,所以我不发送多条消息/事件的单个发送消息事件?

非常感谢球员。


解决方案

  

    

在非SPA将这种行为还是发生了?
    是否在非SPA切换页面时的插座断开连接?


  

在非SPA,当你从一个页面移动到另一个时,浏览器会删除任何连接旧页面仍有打开。每个页面在一个完全不同的上下文中运行。

在一个温泉,你应该将你的套接字创建code到顶层模块,仅运行时,应用程序初始化的东西,但不是当你从一个页面切换到另一个。所以,你会通过应用程序生命周期使用单个socketio实例。

I have yet to find a solution or any information on this topic. Basically I am developing a chat app with flask, socketio, and angularjs. I created a single page app with angularjs so when I click for example a register button this page comes into view even though it is not actually routing to a new page. The problem is when I click the register button or switch views another socket connection gets created. So when I go back to the chat app view to send a message it sends 3 messages because 3 sockets were created. One initially, one when I went to register view and the third socket connection was created when I went back to chat app view. So I have a few questions about this behavior:

In a non-SPA would this behavior still happen?

Does a socket get disconnected when switching pages in a non-SPA?

With my issue I can see that changing views in a SPA creates another socket but it does not delete a socket. How do I avoid having multiple socket connections so I do not send multiple messages/events for a single sent message event?

Thanks a lot guys.

解决方案

In a non-SPA would this behavior still happen? Does a socket get disconnected when switching pages in a non-SPA?

In a non-SPA, when you move from one page to another, the browser deletes any connections the old page still has open. Each page runs in a completely separate context.

In a SPA, you should move your socket creation code to a top-level module, something that runs only when the app initializes, but not when you switch from one page to another. So you will be using a single socketio instance through the life of the application.

这篇关于角单页的应用程序和socketio多个套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆