B4J Question Websocket Client ID

Declan

Well-Known Member
Licensed User
Longtime User
How would I get the ID of the Client when the Client connects to my B4J WebSocket server?
I will have a a number of clients connecting and I would like to be able to send messages from the B4J server to an individual connected client, or to a selection of clients.
Is this possible?
 

Declan

Well-Known Member
Licensed User
Longtime User
Thanks.
If I use:
B4X:
Log("Client ID: " & ws.Session.Id)
I get:
B4X:
Connected: (HashedSession) org.eclipse.jetty.server.session.HashedSession:bsczsvsgssyfn72f4be5pw64@1659791576
Client ID: bsczsvsgssyfn72f4be5pw64
Can I now use the Client ID "bsczsvsgssyfn72f4be5pw64" to send a message to that individual Client?
If so, how would I attach the Client ID to:
B4X:
ws.RunFunction("SendMesg", Array As Object("Hello"))
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
I have looked at both the links sent.
Let me outline an overview of what I am attempting to accomplish:
I have a number of ADT (Articulated Dump Truck) vehicles (~1000) operating in open-cast mines.
All these vehicles have an Android 7" tablet that acts as a Interface with the operators.
These Android devices also obtain various parameters, using USB Serial, from sensors mounted within the vehicle.
I must maintain a permanent connection from the remote devices with my server (2008 R2) - this I have accomplished either using TCP Sockets and a "heartbeat" and also with WebSockets.
I must be able to receive data from the remote devices - This I have no problem with using either TCP Sockets or WebSockets.
I must be able to send a message from the server to all remote devices (broadcast) - This I have no problem with using either TCP Sockets or WebSockets.
I must be able to send a message to an individual remote device - This is where I have a problem.

The overall architecture:
As I have no experience with HTML or web pages, I have written a very simple "Console" web page that the user enters a message into.
I then write this message to a MySQL database.
The Server app (B4J), must then periodically query this database, and if a message is present, then the message is sent, either as a broadcast to all remote devices OR to an individual remote device. Once sent, the message is deleted from the database.

Looking at the above link "Chatroom", this could work, but I would not use the "index.html" nor the "chat.html".
I would like to use the remote device IMEI as the chat "name".
Could this IMEI (name) be sent on initial connection?
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
@Erel - Many thanks.
I have the device connection to the server using its' IMEI as the ID.
On the Server (ServerExampleNoMySQL) I receive:
B4X:
Waiting for debugger to connect...
Program started.
2017-02-26 12:10:04.391:INFO::main: Logging initialized @942ms
2017-02-26 12:10:04.513:INFO:oejs.Server:main: jetty-9.3.z-SNAPSHOT
2017-02-26 12:10:04.585:INFO:oejsh.ContextHandler:main: Started o.e.j.s.ServletContextHandler@50f8360d{/,file:///C:/B4J-ServerExampleNoMySQL/ServerExampleNoMySQL/Objects/www/,AVAILABLE}
2017-02-26 12:10:04.588:INFO:oejs.AbstractNCSARequestLog:main: Opened C:\B4J-ServerExampleNoMySQL\ServerExampleNoMySQL\Objects\logs\b4j-2017_02_26.request.log
2017-02-26 12:10:04.732:INFO:oejs.ServerConnector:main: Started ServerConnector@7cd62f43{HTTP/1.1,[http/1.1]}{0.0.0.0:32009}
2017-02-26 12:10:04.733:INFO:oejs.Server:main: Started @1285ms
Emulated network latency: 200ms
Server started
NewConnection: 358726061334668
So all is well.
How do I now send from the server to the Client?
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
When I send from Client to server with:
B4X:
Sub btnSend_Click
    Dim m As Map
    m.Initialize
    m.Put("Message","Hello from Client")
    PushService.wsh.SendEventToServer("Message", m)
End Sub
I receive the following error on the jServer:
B4X:
Waiting for debugger to connect...
Program started.
2017-02-26 12:30:54.902:INFO::main: Logging initialized @879ms
2017-02-26 12:30:55.004:INFO:oejs.Server:main: jetty-9.3.z-SNAPSHOT
2017-02-26 12:30:55.076:INFO:oejsh.ContextHandler:main: Started o.e.j.s.ServletContextHandler@50f8360d{/,file:///C:/B4J-ServerExampleNoMySQL/ServerExampleNoMySQL/Objects/www/,AVAILABLE}
2017-02-26 12:30:55.078:INFO:oejs.AbstractNCSARequestLog:main: Opened C:\B4J-ServerExampleNoMySQL\ServerExampleNoMySQL\Objects\logs\b4j-2017_02_26.request.log
2017-02-26 12:30:55.206:INFO:oejs.ServerConnector:main: Started ServerConnector@7cd62f43{HTTP/1.1,[http/1.1]}{0.0.0.0:32009}
2017-02-26 12:30:55.207:INFO:oejs.Server:main: Started @1185ms
Emulated network latency: 200ms
Server started
NewConnection: 358726061334668
java.lang.RuntimeException: Invalid event name: Message
    at anywheresoftware.b4j.object.WebSocketModule$Adapter.onWebSocketText(WebSocketModule.java:115)
    at org.eclipse.jetty.websocket.common.events.JettyListenerEventDriver.onTextMessage(JettyListenerEventDriver.java:189)
    at org.eclipse.jetty.websocket.common.message.SimpleTextMessage.messageComplete(SimpleTextMessage.java:69)
    at org.eclipse.jetty.websocket.common.events.AbstractEventDriver.appendMessage(AbstractEventDriver.java:66)
    at org.eclipse.jetty.websocket.common.events.JettyListenerEventDriver.onTextFrame(JettyListenerEventDriver.java:158)
    at org.eclipse.jetty.websocket.common.events.AbstractEventDriver.incomingFrame(AbstractEventDriver.java:162)
    at org.eclipse.jetty.websocket.common.WebSocketSession.incomingFrame(WebSocketSession.java:367)
    at org.eclipse.jetty.websocket.common.extensions.ExtensionStack.incomingFrame(ExtensionStack.java:214)
    at org.eclipse.jetty.websocket.common.Parser.notifyFrame(Parser.java:220)
    at org.eclipse.jetty.websocket.common.Parser.parse(Parser.java:256)
    at org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection.readParse(AbstractWebSocketConnection.java:663)
    at org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection.onFillable(AbstractWebSocketConnection.java:493)
    at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:261)
    at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:95)
    at org.eclipse.jetty.io.SelectChannelEndPoint$2.run(SelectChannelEndPoint.java:75)
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceAndRun(ExecuteProduceConsume.java:213)
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:147)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:654)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:572)
    at java.lang.Thread.run(Thread.java:745)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
Cool.
On my Server side I have:
B4X:
'not used. Allows the device to send messages
Public Sub Device_MessageFrom(Data As Map)
    Log("Message From Device: " & Data.Get("text"))
'    CallSubDelayed3(PushShared, "NewMessage", Data.Get("text"), Null)
End Sub
This receives the messages from the remote devices. (I do not need to send this anywhere - I will write into a MySQL DB).

On the B4A side, I have:
B4X:
    Dim m As Map
    m.Initialize
    m.Put("text","Hello from Client")
    PushService.wsh.SendEventToServer("Device_MessageFrom", m)
Working well and sending message to server.

I will look at "replacing" the SQLite DB with a MySQL DB on the server side.
I can then write messages (to send to remote devices) to this MySQL DB from a web page.

Using the "ServerExampleNoMySQL" to build this, there are a number of modules that I do not require.
How do I delete these modules from the B4J app?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I will look at "replacing" the SQLite DB with a MySQL DB on the server side.
I can then write messages (to send to remote devices) to this MySQL DB from a web page.
You don't need to replace the database. You already have a web server. Check PushBrowser module.

How do I delete these modules from the B4J app?
Project - Remove Module.
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
The reason I would like to change the database is that I will be writing the messages to send into the database from a Webpage.
I would need to query the database in that if the user is connected, I must send the message immediately.
If the user is not connected, then I write the message etc. into the messages_to-Deliver table.
I am familiar with MySQL databases.
 
Upvote 0
Top