B4J Question WebSocket question from a newbie

Hi Guys
I apologise for what is probably quite a dumb question.

This is my first application written in B4J, I have written a few VB.Net applications in the past, but am quite a novice at programming - so I have probably bitten off more than I can chew. I am trying to write a GUI application to interface with LinuxCNC. Both LinuxCNC and my application will run on a Linux PC.

A webserver has been written for the LinuxCNC side of things (https://github.com/MachineryScience/Rockhopper), and so “all” I need to do is to write a client application which opens a WebSocket with the LinuxCNC server, and send(and receive) JSON formatted text commands.

Now for my dumb question…..Can I write (or possibly I should say, is it possible to write) a desktop application to do this without the need for an external webserver?

I have done a brief read up of the ABMaterial framework and believe this may be a way I can achieve what I want, but initially I would love to be able to do it as a standard desktop app, using the internal designer.

Thanks for your help.
Andrew
 
Hi Erel,
Thanks heaps for your reply. I had read the thread you referred to, and so had an inkling that it could be done but wasn't 100% sure.
I will start with the WebSocketPush application as a base, and try and add to it.
 
Upvote 0
Hi
I am having a bit of trouble understanding the correct connection path I need to specify in B4J.

In JavaScript the correct connection method would be:
ws = new WebSocket("ws://192.168.1.1:8000/websocket/linuxcnc","linuxcnc");
Where the second "linuxcnc" specifies the sub-protocol.

So in the B4J example I have put the following code:
Public serverLink As String = "ws://192.168.1.1:8000/websocket/linuxcnc"

Where do I specify the sub_protocol?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I will start with the WebSocketPush application as a base, and try and add to it.
This is a good example if you are interacting with B4J WebSockets server. It is less relevant for communication with other servers.

Where do I specify the sub_protocol?
I can add it to the library. What happens if you try to connect without specifying the subprotocols?
 
Upvote 0
Hi
I THINK it is connecting. I am getting a message on the LinuxCNC side of things saying "WebSocket connected", but any of the commands I try and send don't seem to work, and I don't know if this is because I need to log onto the server first of if the commands aren't formatted correctly of if the sub-protocol has something to do with it.

The server requires the client to log on with: username = default and password = default.
I am unsure how this needs to be sent - I have tried just sending a text string but that doesn't seem to be working, but I am not 100% certain my commands are getting through.
As a trial I connected to the server through a browser and did the login procedure there, but still no commands seem to get through. So I am unsure what I can do to test if the commands are actually getting to the server, even though it says it is connected. I am trying to send the command as a text string as below:

Dim command as String
command = " { ""id"": ""my-id"", ""command"":""put"", ""name"":""state"", ""state"":""STATE_ESTOP_RESET"" }"
ws.SendText(command)

Any pointers and advise would be greatly appreciated.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Dim command as String
command = " { ""id"": ""my-id"", ""command"":""put"", ""name"":""state"", ""state"":""STATE_ESTOP_RESET"" }"
Such commands should be built with the JSON library, though it is not related to the problem.

Your need to find the instructions in the server documentation. It is impossible to guess the protocol.
 
Upvote 0
Top