B4J Question [RESOLVED]WebSockets Help

Jmu5667

Well-Known Member
Licensed User
Longtime User
Hi All

I am trying to develop a jserver app that will accept web socket connections. My understanding is that jserver is HTTP based. I do not what to use the HTTP aspect of this as I want to send and received data from the clients in a raw format as this is not a web app with HTMl pages etc.

Why do I want to do this ? Most of the apps we have are VB6 winsock base apps. So, we need to use the multi-threading aspect of jserver to provide a more robust solution for our customers and not be limited to running all socket connects on the main thread.

I have very little experience with HTTP servers etc so please forgive my ignorance on this topic.

This is the jserver setup

B4X:
Sub initialise_host

   CallSubDelayed2(mod_app_functions,"writelog","initialise_host(), not Initialized")
   
   ' // initialise the server host
   Try
     svrHost.Initialize("host")
     svrHost.Port = appSet.svr.port
     svrHost.StaticFilesFolder = appSet.settingsLocation
     ' // root folder
     svrHost.AddWebSocket("/ws","class_websocket")
     svrHost.Start
     appSet.svr.started = True
     CallSubDelayed2(mod_app_functions,"writelog","initialise_host(), server started")
   Catch
     appSet.svr.started = False
     CallSubDelayed2(mod_app_functions,"writelog","initialise_host(), error - " & LastException.Message )
   End Try

End Sub

The /ws is an empty folder on the root drive (windows 7).

This is the class_websocket

B4X:
'WebSocket class
Sub Class_Globals
   
   Private ws As WebSocket
   
End Sub

Public Sub Initialize
   
   CallSubDelayed2(mod_app_functions,"writelog","WebSocket_Initialized()")
   
End Sub

Private Sub WebSocket_Connected (WebSocket1 As WebSocket)

   ws = WebSocket1
   
   CallSubDelayed2(mod_app_functions,"writelog","WebSocket_Connected()")

End Sub

Private Sub WebSocket_Disconnected

   CallSubDelayed2(mod_app_functions,"writelog","WebSocket_Disconnected()")

End Sub


Using a Vb6 winsock test app or HyperTerminal I can connect to the server but the class_socket does not seem to get executed.

Once I have a connection established I would like know to to send and receive data on each web-socket thread.

I would be very grateful with any assistance on this topic. Once I have this solved I would be quite happy to publish a basic template project to help others who want to achieve the same.

Regards

John.
 

wl

Well-Known Member
Licensed User
Longtime User
I guess you need to Base64 encode the binary data.

Raw sockets would be a better choice, but you are correct that raw sockets do not work multi-threaded in B4J
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
I guess you need to Base64 encode the binary data.

Raw sockets would be a better choice, but you are correct that raw sockets do not work multi-threaded in B4J
Hi wl

That maybe the case alright, however I cant get the class_websocket to fire when a connetion is made .... I need to get all the comms working first before I can send and receive data.

Have you built a jserver app with the websocket class ?

Regards

John.
 
Upvote 0

wl

Well-Known Member
Licensed User
Longtime User
Hi John,

I indeed wrote a websocket app. Although the info is a bit scattered throughout the B4 and B4J forums my main info was the websocket chat app.
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
I have looked at that alright. I have a very simple test app, init the jserver and add websocket but when the connection is made the web socket class is never called ?
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
when the server starts this is the log data

2015-02-25 13:33:05.367:INFO::main: Logging initialized @155ms
2015-02-25 13:33:05.520:INFO:eek:ejs.Server:main: jetty-9.1.z-SNAPSHOT
2015-02-25 13:33:05.572:WARN:eek:ejh.MimeTypes:main: java.util.MissingResourceException: Can't find bundle for base name org/eclipse/jetty/http/encoding, locale en_IE
2015-02-25 13:33:05.611:INFO:eek:ejsh.ContextHandler:main: Started o.e.j.s.ServletContextHandler@1016012{/,file:/C:/Users/John%20Murphy/Desktop/Documents/B4J/test%20jserver/Objects/www,AVAILABLE}
2015-02-25 13:33:05.616:INFO:eek:ejs.AbstractNCSARequestLog:main: Opened C:\Users\John Murphy\Desktop\Documents\B4J\test jserver\Objects\logs\b4j-2015_02_25.request.log
2015-02-25 13:33:06.724:INFO:eek:ejs.ServerConnector:main: Started ServerConnector@29cab9{HTTP/1.1}{0.0.0.0:8087}
2015-02-25 13:33:06.724:INFO:eek:ejs.Server:main: Started @1646ms

My ip address seems to be {0.0.0.0:8087}, even though it is 192.168.137.1
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
WebSockets will only work with a web socket client.

Can you send http requests from the VB6 apps?

Note that if you are using AsyncStreams with regular sockets then the communication is already handled in the background for you. You do not need to create more threads.
This is not scalable as a solution based on jServer, however if you do not have full control over the client then you may have no other choice.
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
WebSockets will only work with a web socket client.

Can you send http requests from the VB6 apps?

Note that if you are using AsyncStreams with regular sockets then the communication is already handled in the background for you. You do not need to create more threads.
This is not scalable as a solution based on jServer, however if you do not have full control over the client then you may have no other choice.

Hi Erel

thanks for the heads up. The B4A app currently use the standard SOCKET, I will upgrade it to use websockets and start from there. As long as I can send and received any type of data from the client to the server that's good enough for me.

It should not be too difficult to do this...fingers crossed !

Regards

John.
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
I'm not sure that you should use WebSockets instead of standard servlet handlers.

Do you need to maintain a persistent connection? Or the clients send some data each time and receive the server response?
I need to maintain a persistent connection, data can be sent and received at anytime.
 
Upvote 0
Top