Websocket communication - I have gaps in my knowledge!

I'm trying to make an Uno w/Ethernet talk to some websockets using socket.io and json. Someone else is setting up the server, so using Johnny-Five is really not an option.

All the Arduino examples I can find are expecting me to know more about this subject than I do. I know what all the parts are and most of how they work together, but to put together the actual data packet to send so the server understands what I sent is where my knowledge fails me. Also the other side of the coin, deciphering the messages sent to me.

And I can't seem to find those answers.

Can someone point me in the right direction?

I don’t have any answers but editing your Original Post to change the title to “Help with Ethernet Shield and websockets” or something similar might be more productive.

…R

All the Arduino examples I can find are expecting me to know more about this subject than I do.

That's life for you.

Can someone point me in the right direction?

It's the one that is not the left direction.

You have said "there is some stuff I do not know". Now, you are asking us to reach you that stuff. Where should we start? How are you on performing open heart surgery? Playing tennis? Deep-sea diving? Wood turning?

Robin2: I don't have any answers but editing your Original Post to change the title to "Help with Ethernet Shield and websockets" or something similar might be more productive.

...R

Point taken. In hindsight, it was not conveying the message I was thinking. Time constraints, holidays, and a severe head cold don't mix well when trying to communicate!

PaulS: You have said "there is some stuff I do not know". Now, you are asking us to reach you that stuff. Where should we start? How are you on performing open heart surgery? Playing tennis? Deep-sea diving? Wood turning?

What I need to know, in regards to my original post, is how to format the data I need to send (although wood turning might be good too, just not as pressing at the moment).

What I need is a resource (book, website, forum post, something) that will show me how to format that data. I know how to send TCP messages, and I understand what a websocket is, and how socket.io and json are intended to work with one. What I am trying to understand is how to make that work on an Arduino - either through a library or brute-force low-level encoding of the TCP message.

Like I said, what I have found for on-line examples so far assume the reader has more knowledge than I do. I'm not even sure what it is I do not know. I am trying to fill in those gaps in my knowledge by asking for help. If anyone knows where I can find the information I need, please let me know. If not, just let this post sink to the bottom of of the bit-bucket.

Update:

I know a little more than I did this morning on the subject. If I do end up answering my own question, I will post what I learned here so others with the same question can learn from this experience.

If I have a script running on a node server that looks like this:

   socket.on('level', function(level) {

It appears if I want to tell it the level is currently 123, then the command message I want to send will be something along the line of:

["level",{"data":"123"}]

At least that is what is emerging from the fog my brain has been this week. I will be trying that this afternoon, but if there is an error in my thinking, please point it out!

will be something along the line of:

Unlikely. json consists of objects, arrays, and lists. What you have described is a list with an object and an array. That is not possible. All the things in a list have to be of the same type.

[{"level":"123"}] might work.

It might be easier to learn how to do what you wnt with a PC program - simply because there will be a lot more online advice for PC based web programming.

I suspect when you know how to do it that you will have little trouble adapting it to an Arduino (assuming it is possible on an Arduino).

...R

PaulS: [{"level":"123"}] might work.

Closer, yes. Actually it turns out the number doesn't need to be in quotes as the script at the other end expects an integer. The brackets [] aren't necessary either I guess. That I got from conversing with the programmer who wrote the javascript file I'm trying to communicate with. Unfortunately he doesn't know much about how it actually works (other than it does), so he hasn't been able to help much on this.

Robin2: It might be easier to learn how to do what you wnt with a PC program - simply because there will be a lot more online advice for PC based web programming.

I suspect when you know how to do it that you will have little trouble adapting it to an Arduino (assuming it is possible on an Arduino).

...R

That's pretty much what I've been doing over Christmas. The gaps in my knowledge are getting smaller, but as they get smaller I realize how much bigger they were to begin with.

Currently I'm working on how to make the connection through socket.io so it will actually listen to what I have to say...

Update:

I am still not able to connect to a socket.io server, and I’m beginning to wonder if the changes between the <Ethernet.h> and the <Ethernet2.h> libraries introduced something. Sample codes from various libraries cannot connect to either remote or local servers.

Here is what I can see with a packet sniffer when connecting to a local machine running a node server with a simple socket.io script looking for JSON data. The Arduino is using Bill Roy’s SocketIOClient library.

I see the Arduino try to open the communication, which the PC acknowledges, then 84 TCP messages going back and forth between the Arduino and the PC. The PC is just sending an acknowledgement and the Arduino sending mostly just linefeeds and the occasional carriage return.

Then a message from the PC:

HTTP/1.1 400 Bad Request
Content-Type: application/json
Hypertext Transfer Protocol
HTTP/1.1 400 Bad Request
Content-Type: application/json
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: Arduino
Date: Thu, 29 Dec 2016 16:52:01 GMT
Connection: keep-alive
Transfer-Encoding: chunked
{“code”: 0,“message”:“Transport unknown”}

And then both signal to disconnect.

This raises two questions about the failure to communicate.

Is the server terminating the connection because the Arduino is not sending anything? (failure on the Arduino side)

Is the Arduino waiting for something from the server? (failure on the server side)

Anyone have an ideas?

BTW, I did find the SocketIOClient library calls the standard Ethernet library, and I changed that to Ethernet2 (which gave the compiler a fit before I changed that).

EDIT: I found an older shield and tried it again with the older Ethernet.h library, and no change.
However, I did accidently try talking with the wrong port number and got this response:

Not connected.
Drop HTTP/1.1 400 Bad Request
Drop Content-Type: text/html; charset=us-ascii
Drop Server: Microsoft-HTTPAPI/2.0
Drop Date: Thu, 29 Dec 2016 21:51:24 GMT
Drop Connection: close
Drop Content-Length: 326
Drop
Drop
Drop Bad Request
Drop
Drop

Bad Request - Invalid Verb


Drop

HTTP Error 400. The request verb is invalid.


Drop

Basically, it connects and the Arduino offers a handshake, but gets this as a reply.

Well, this is a bummer. I got in touch with Bill Roy, who authored one of the more detailed libraries, and he thinks there were some changes in the socket.io protocal in the time since he had created the library. Since all the libraries I found were about the same age, right now it is at a dead end - short of writing a new library. Bill was going to check to see if it was a simple fix and see about an update, but I'm at a bit of a standstill right now. :-\

Everything that happens on the web involves sending text from one place to another. If you know the text that needs to be sent to make something happen then it should be possible to make an Arduino send it - provided, of course, that the text will fit in an Arduino.

...R

And the $64K question is...what does it send? :-\

(If I knew that I would have brute-forced this already!)

MichaelL65: And the $64K question is...what does it send? :-\

I don't know - I have never tried to do web-socket programming. But, as I said earlier, I don't think this Forum is the ideal place to get that sort of advice.

...R