B4J Question Web socket server - fail connect clients with header "Sec-WebSocket-Protocol"

EvgenyB4A

Active Member
Licensed User
Longtime User
I use ServerExampleNoMySQL web server app to get connections from websocket clients, for example Chrome extension "Browser WebSocket Client".
The client succeeds to connect to my server when the Server Protocol field("Sec-WebSocket-Protocol") is empty.
When I assign any value to the heder the server outputs with following and doesn't connect:

Waiting for debugger to connect...
Program started.
2023-06-21 10:50:05.443:INFO::main: Logging initialized @740ms to org.eclipse.jetty.util.log.StdErrLog
2023-06-21 10:50:05.580:INFO:eek:ejs.Server:main: jetty-9.4.z-SNAPSHOT; built: 2018-05-03T15:56:21.710Z; git: daa59876e6f384329b122929e70a80934569428c; jvm 11.0.1+13
2023-06-21 10:50:05.627:INFO:eek:ejs.session:main: DefaultSessionIdManager workerName=node0
2023-06-21 10:50:05.627:INFO:eek:ejs.session:main: No SessionScavenger set, using defaults
2023-06-21 10:50:05.629:INFO:eek:ejs.session:main: node0 Scavenging every 600000ms
2023-06-21 10:50:05.677:INFO:eek:ejsh.ContextHandler:main: Started o.e.j.s.ServletContextHandler@366647c2{/,file:///C:/B4J/Examples/ServerExampleNoMySQL%20(2)/ServerExampleNoMySQL/Objects/www/,AVAILABLE}
2023-06-21 10:50:05.688:INFO:eek:ejs.AbstractNCSARequestLog:main: Opened C:\B4J\Examples\ServerExampleNoMySQL (2)\ServerExampleNoMySQL\Objects\logs\b4j-2023_06_21.request.log
2023-06-21 10:50:05.930:INFO:eek:ejs.AbstractConnector:main: Started ServerConnector@581ac8a8{HTTP/1.1,[http/1.1]}{0.0.0.0:51052}
2023-06-21 10:50:05.930:INFO:eek:ejs.Server:main: Started @1230ms
Emulated network latency: 200ms
Server started
URL: /push/b4a_ws2
Header: abcd
java.lang.RuntimeException: org.eclipse.jetty.websocket.api.WebSocketException: RemoteEndpoint unavailable, current state [CLOSED], expecting [OPEN or CONNECTED]
at anywheresoftware.b4j.object.WebSocket.setEvents(WebSocket.java:378)
at anywheresoftware.b4j.object.WebSocketModule$Adapter$ThreadHandler.run(WebSocketModule.java:186)
at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:47)
at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:43)
at anywheresoftware.b4a.shell.ShellBA.startMessageLoop(ShellBA.java:119)
at anywheresoftware.b4a.keywords.Common.StartMessageLoop(Common.java:175)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:309)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:109)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:96)
at b4j.example.main.main(main.java:29)
Caused by: org.eclipse.jetty.websocket.api.WebSocketException: RemoteEndpoint unavailable, current state [CLOSED], expecting [OPEN or CONNECTED]
at org.eclipse.jetty.websocket.common.WebSocketSession.getRemote(WebSocketSession.java:411)
at anywheresoftware.b4j.object.WebSocket.sendText(WebSocket.java:107)
at anywheresoftware.b4j.object.WebSocket.setEvents(WebSocket.java:375)
... 15 more


It doesn't reach to Sub WebSocket_Connected (WebSocket1 As WebSocket)

How to solve the problem?
 

EvgenyB4A

Active Member
Licensed User
Longtime User
The problem is not in browser.
I need to build the web socket server to which the clients connect with Header "Sec-WebSocket-Protocol".
The chrome extension allows to set this header this:
1687338067574.png


The server side fails when this header has any value.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
The chrome extension allows to set this header this:
Please note that according to a stackoverflow.com post, chat is not a proper subprotocol of WebSocket, therefore it looks like jServer is behaving as expected (with a non-valid requested subprotocol).

Stackoverflow.com post: https://stackoverflow.com/questions...f-chat-and-superchat-subprotocol-in-websocket
Linked (in above post) valid subprotocols: https://www.iana.org/assignments/websocket/websocket.xml#subprotocol-name
 
Upvote 0

EvgenyB4A

Active Member
Licensed User
Longtime User
Hi Oliver,
From the first link you mentioned:
"The protocol header itself is completely optional, but meant to provide a way to document what sort of dialogue/application this connection is supposed to handle."
So, jServer shouldn't react with Runtime exceptions.
As to the second link, I try with "ocpp1.6" value(looks standard) - with same result.
The server I build should accept incoming connections from the clients with header "Sec-WebSocket-Protocol" and its value "ocpp1.6".
I tried from Browser Websocket Client to connect to other servers with this header and did got connection from them.
 
Upvote 0

EvgenyB4A

Active Member
Licensed User
Longtime User
What exactly are you trying to do?
Hi Erel,
I build the WebSocket server on the base of jServer to serve clients(EV charging stations). The standard obligates clients to include header "Sec-WebSocket-Protocol" with value "ocpp1.6" for the connection to be accepted. The "Browser Websocket Client" serves as test tool to check the servers. I tried to connect to other servers with this header and did got connection from them.
 
Upvote 0

EvgenyB4A

Active Member
Licensed User
Longtime User
I tried to connect to my server with program based on WebSocketPush example with following:

Public Sub wsConnect(Url As String)
Dim UpgradeRequest As JavaObject
UpgradeRequest.InitializeNewInstance("org.eclipse.jetty.websocket.client.ClientUpgradeRequest", Null)
UpgradeRequest.RunMethod("setHeader", Array("Sec-WebSocket-Protocol", "ocpp1.6"))
ws.Connect2(Url, UpgradeRequest)

' ws.Connect(Url)
End Sub
and I got connection.
What may be different between jetty client and the client of chrome extension?
I'm afraid that my server could not serve many clients.
 
Upvote 0
Top