B4J Question WebSocketClient

Dominik Böhm

Member
Licensed User
Longtime 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
 

Dominik Böhm

Member
Licensed User
Longtime 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.
 
Upvote 0

Dominik Böhm

Member
Licensed User
Longtime 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 );
}
 
Upvote 0

Dominik Böhm

Member
Licensed User
Longtime 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 ;)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime 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
 
Upvote 0

Dominik Böhm

Member
Licensed User
Longtime User
sorry. I forgot to answer. I've found a solution that solves the problem for me. perhaps someone else have the same problem.

i used the wrapper from the internal b4j websocket and changed it to work with the websocket (v1.4.0) from https://github.com/TooTallNate/Java-WebSocket
because i dont know how to change the protocolDraft in b4j i hardcoded it in the source file.

WebSocketClientWrapper.java:
        URI echoUri = new URI(Url);

//        How to change this in B4J?
        Draft_6455 protocolDraft = new Draft_6455(Collections.<IExtension>emptyList(), Collections.<IProtocol>singletonList(new Protocol("MyName")));
//        Draft_6455 protocolDraft = new Draft_6455();

        wsc = new WebSocketClient(echoUri, protocolDraft, httpHeaders, timeout) {

To change the log level add this in b4j
B4X:
Sub AppStart (Args() As String)
...
inline.RunMethod("setLevel", Array("WARN"))
...
End Sub

Sub inline As JavaObject
    Return Me
End Sub

#if java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public static void setLevel(String LogLevel){
System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, LogLevel);
 }
#End If


Sample code
B4X:
Sub Class_Globals
Private wsc         As WebSocketClient
End Sub

Public Sub Initialize
End Sub

Public Sub Connect(Host As String, TimeOut As Int)
    Dim m1 As Map
    m1.Initialize
    wsc.Initialize("wsc", Host, m1, TimeOut)
    wsc.Connect
End Sub

Sub wsc_Open (o1 As Object)
End Sub
Sub wsc_Close (code As Int, reason As String, remote As Boolean)
End Sub
Sub wsc_Error
    Log(LastException.Message)
End Sub
Sub wsc_Message (Message As String)
    Log(Message)
End Sub
Public Sub SendMessage(s As String)
    esc.SendText(s)
End Sub
 

Attachments

  • libWebSocket.zip
    3.3 KB · Views: 277
  • srcWebSocket.zip
    1.9 KB · Views: 274
Upvote 0
Top