话说SockJS对于每个domain只能创建一个对象,那就是说,在整个应用中,客户端浏览器对于单一domain只拥有一个SockJS的对象,意味着整个应用中只有一行全局变量 var socket = new SockJS("url"); 对于这个要求,怎么才能达到??You know,即使只有一个js中有这行代码,但是html,jsp这些文件中每个文件里面都会引用这个js,那么最终还不是有多少个html或者jsp被访问,就有多少个SockJS对象被创建吗?
具体原因看下面的描述
https://github.com/sockjs/sockjs-client
Although the 'SockJS' object tries to emulate the 'WebSocket' behaviour, it's impossible to support all features. One of the important SockJS limitations is the fact that you're not allowed to open more than one SockJS connection to a single domain at a time. This limitation is caused by a in-browser limit of outgoing connections - usually browsers don't allow opening more than two outgoing connections to a single domain. Single SockJS session requires those two connections - one for downloading data, the other for sending messages. Opening a second SockJS session at the same time would most probably block and can result in both sessions timing out.