SergeZ
SergeZ

Reputation: 408

Creating multiple WebSockets

Can someone tell me what would happen If I create more than one WebSocket and try to use them to send some data to server?
Will this work or will I get some error?

P.S.: I ask this question because of I use the GWT framework... Client code is written in pure Java (actually the client-side emulated subset of pure Java) while JavaScript is used in order to create the webSocket connections.
But I want to use webSocket in some other place too, so either I must rely on order of code execution. Secondly I think to reuse the already created JavaScript webSocket.

Upvotes: 1

Views: 138

Answers (2)

SergeZ
SergeZ

Reputation: 408

All that is necessary to do in order to get what I demand is to write:

$wnd.socketVar = new $wnd.WebSocket(server1)

In any other place it is possible to access exactly that web socket throught: $wnd.socketVar.

Upvotes: 0

oberstet
oberstet

Reputation: 22011

Does that answer your question?

var socket1 = new window.WebSocket("ws://server1.com");
var socket2 = new window.WebSocket("ws://server2.com");

socket1.send("hello 1"); // will send to server1.com
socket2.send("hello 2"); // will send to server2.com

You send via a method on a specific WebSocket object.

Upvotes: 1

Related Questions