B4J Question WebSocketClient

Dominik Böhm

New Member
Licensed User
Hi

i want to setup web WebSocket connection to a server. i need to set the protocol for this like in javascript.
WebSocket WebSocket(
in DOMString url,
in optional DOMString protocols
);
how could i do this with the jWebSocketClient Library?

Thanks
Dominik
 

Erel

Administrator
Staff member
Licensed User
Not sure that I understand the question. You should pass the url to the Connect method.
 

Dominik Böhm

New Member
Licensed User
this is the code from js. from an example.
ws = new WebSocket("ws://"+Host+":2000", Proto);
ws.onopen = function() {
ws.send("SOMEDATA");
...
my code
ws.Initialize("WebSocket")
ws.Connect("ws://192.168.10.6:2000")
result is an error org.eclipse.jetty.websocket.api.UpgradeException: 400 Bad Request

the code in js seems is working.
 

Dominik Böhm

New Member
Licensed User
mhh. no chance. always the same error msg.

i found this in the WebSocketClient.java. Could i set the protocolDraft from B4J?

main/java/org/java_websocket/client/WebSocketClient.java
/**
* Constructs a WebSocketClient instance and sets it to the connect to the
* specified URI. The channel does not attampt to connect automatically. The connection
* will be established once you call <var>connect</var>.
* @param serverUri the server URI to connect to
* @param protocolDraft The draft which should be used for this connection
*/
public WebSocketClient( URI serverUri , Draft protocolDraft ) {
this( serverUri, protocolDraft, null, 0 );
}
 

Dominik Böhm

New Member
Licensed User
its a lan controller card with its own webserver. no idea why they need a special protocol.
nothing special in the communication. i send a refresh command and the controller response with xml data.
all plain text ;)
 

Erel

Administrator
Staff member
Licensed User
If it is a standard WebSocket server then the connection will work. No need to do anything special.

Full example:
B4X:
Sub Process_Globals
    Private wsc As WebSocketClient
End Sub

Sub AppStart (Args() As String)
    Connect
    StartMessageLoop
End Sub

Sub Connect
    wsc.Initialize("wsc")
    wsc.Connect("wss://b4x.com:51041/smiley/ws")
    Wait For wsc_Connected
    Log("connected")
End Sub
 
Top