My webpage loads a background flash which will connect to the server using socket. If a user open multiple windows/tabs of my website, the server will get multiple socket connections. Any idea of how to make sure that only one socket is connected from the same user and same browser?
我的网页加载了一个后台闪存,它将使用套接字连接到服务器。如果用户打开我的网站的多个窗口/标签,服务器将获得多个套接字连接。知道如何确保只有一个套接字从同一个用户和同一个浏览器连接?
I am thinking of using Javascript to monitor the window close event, if a window that was connected to the server is closed, one of the other windows will try to connect to the server. But I can't find a way to listen to that event.
我正在考虑使用Javascript来监视窗口关闭事件,如果连接到服务器的窗口关闭,其他窗口之一将尝试连接到服务器。但是我找不到听这个事件的方法。
I was thinking of Flash's LocalConnection too, but can't find a way to assign unique connection names and let other Flashes know.
我也在考虑Flash的LocalConnection,但找不到分配唯一连接名称的方法,让其他Flashes知道。
3 个解决方案
#1
You might be able to do it from within Flash by using LocalConnection.
您可以使用LocalConnection在Flash中执行此操作。
When a movie loads, try sending a test message to some LocalConnection ID. If that fails then the connection isn't open, in which case you open it to listen for commands, and you connect to the server via the socket.
加载影片时,尝试将测试邮件发送到某个LocalConnection ID。如果失败则连接未打开,在这种情况下,您打开它以侦听命令,并通过套接字连接到服务器。
If it succeeds then there's already a SWF running that's connected to the server. You can send any server requests that SWF gets over the LocalConnection to the one that's connected to the server.
如果成功,那么已经有一个连接到服务器的SWF正在运行。您可以将SWF通过LocalConnection获取的任何服务器请求发送到连接到服务器的请求。
If you need to get responses back then you may need to have the other SWFs open local connections of their own (random ID) and register them with the SWF that's connected to the server.
如果您需要获得响应,那么您可能需要让其他SWF打开自己的本地连接(随机ID)并将其注册到连接到服务器的SWF。
When the connected SWF gets shut down (window closed) all sends to that LocalConnection should fail. In which case your other SWFs would start getting errors. One of them would have to open that connection and become the "main" SWF (first one to successfully open the connection).
当连接的SWF关闭(窗口关闭)时,对该LocalConnection的所有发送都将失败。在这种情况下,您的其他SWF将开始出错。其中一个必须打开该连接并成为“主要”SWF(成功打开连接的第一个)。
The lifecycle would be a bit weird, and there are probably a lot of corner cases you'd need to handle to make sure it stays robust, but it's an option. However, I've noticed that LocalConnection can become unreliable as the number of connections increases, so you'd probably be better off finding a different solution.
生命周期有点奇怪,并且可能需要处理许多极端情况以确保它保持稳健,但它是一种选择。但是,我注意到随着连接数量的增加,LocalConnection可能会变得不可靠,因此您可能最好找到不同的解决方案。
#2
You can only control the windows you open yourself. There's no client-side way of controlling the other windows/tabs (and no server-side way to distinguish between them either).
您只能控制自己打开的窗户。没有客户端方法来控制其他窗口/标签(也没有服务器端方式来区分它们)。
#3
The event you are looking for is called unload. Assuming you are using window.open to launch the window you might be able to do this:-
您正在寻找的事件称为卸载。假设您使用window.open启动窗口,您可以这样做: -
var win = window.open(url)
if (win.attachEvent)
win.attachEvent("onunload", function() { // stuff when window closes });
else
win.addEventListener("unload", function() { // stuff when window closes }, true);
However from bitter experience relying on unload events can be somewhat unreliable.
然而,依靠卸载事件的痛苦经历可能有些不可靠。
Edit
If the user opens these windows by their own means then the answer is a simple no, this can't be done.
如果用户通过他们自己的方式打开这些窗口,那么答案是简单的否,这是不可能做到的。
#1
You might be able to do it from within Flash by using LocalConnection.
您可以使用LocalConnection在Flash中执行此操作。
When a movie loads, try sending a test message to some LocalConnection ID. If that fails then the connection isn't open, in which case you open it to listen for commands, and you connect to the server via the socket.
加载影片时,尝试将测试邮件发送到某个LocalConnection ID。如果失败则连接未打开,在这种情况下,您打开它以侦听命令,并通过套接字连接到服务器。
If it succeeds then there's already a SWF running that's connected to the server. You can send any server requests that SWF gets over the LocalConnection to the one that's connected to the server.
如果成功,那么已经有一个连接到服务器的SWF正在运行。您可以将SWF通过LocalConnection获取的任何服务器请求发送到连接到服务器的请求。
If you need to get responses back then you may need to have the other SWFs open local connections of their own (random ID) and register them with the SWF that's connected to the server.
如果您需要获得响应,那么您可能需要让其他SWF打开自己的本地连接(随机ID)并将其注册到连接到服务器的SWF。
When the connected SWF gets shut down (window closed) all sends to that LocalConnection should fail. In which case your other SWFs would start getting errors. One of them would have to open that connection and become the "main" SWF (first one to successfully open the connection).
当连接的SWF关闭(窗口关闭)时,对该LocalConnection的所有发送都将失败。在这种情况下,您的其他SWF将开始出错。其中一个必须打开该连接并成为“主要”SWF(成功打开连接的第一个)。
The lifecycle would be a bit weird, and there are probably a lot of corner cases you'd need to handle to make sure it stays robust, but it's an option. However, I've noticed that LocalConnection can become unreliable as the number of connections increases, so you'd probably be better off finding a different solution.
生命周期有点奇怪,并且可能需要处理许多极端情况以确保它保持稳健,但它是一种选择。但是,我注意到随着连接数量的增加,LocalConnection可能会变得不可靠,因此您可能最好找到不同的解决方案。
#2
You can only control the windows you open yourself. There's no client-side way of controlling the other windows/tabs (and no server-side way to distinguish between them either).
您只能控制自己打开的窗户。没有客户端方法来控制其他窗口/标签(也没有服务器端方式来区分它们)。
#3
The event you are looking for is called unload. Assuming you are using window.open to launch the window you might be able to do this:-
您正在寻找的事件称为卸载。假设您使用window.open启动窗口,您可以这样做: -
var win = window.open(url)
if (win.attachEvent)
win.attachEvent("onunload", function() { // stuff when window closes });
else
win.addEventListener("unload", function() { // stuff when window closes }, true);
However from bitter experience relying on unload events can be somewhat unreliable.
然而,依靠卸载事件的痛苦经历可能有些不可靠。
Edit
If the user opens these windows by their own means then the answer is a simple no, this can't be done.
如果用户通过他们自己的方式打开这些窗口,那么答案是简单的否,这是不可能做到的。