ComputerCraft Forums

ComputerCraft => Ask a Pro => Topic started by: forced on Dec 09, 2020, 03:46 PM

Title: http.websocket closing when receiving data?
Post by: forced on Dec 09, 2020, 03:46 PM
I have a problem where I cant seem to receive data from my server(I can send from the client to the server just fine)

It looks like when I try and do ws.receive() it closes the websocket

Any ideas?

(https://camo.tmpim.com/6b748129cd423fde808415891837f5155ce8c108/68747470733a2f2f692e6779617a6f2e636f6d2f31643263316138303431306638656664363161303637613536323262303338652e706e67)

LUA

local ws, err = http.websocket("ws://192.168.0.99:43509")   
 
if err then
    print(err)
elseif ws then
    print("Handshake accepted")
    while true do
        ws.send(io.read("*l"))
        local echo = ws.receive(100)
        if echo == nil then
            print("Echo: NILL")
        else
            print("Echo: "..echo)
        end
    end
end 


Server (C#)

string decodedData = NetworkHandler.DecodeMessage(recievedData);

if(decodedData != "")
    Console.WriteLine(decodedData);

var returnBytes = Encoding.UTF8.GetBytes(decodedData);
client.stream.Write(returnBytes, 0, returnBytes.Length);


NetworkHandler.DecodeMessage is a function that refers to the way mozilla decodes messages

https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_server#Handshaking
Title: http.websocket closing when receiving data?
Post by: QuickMuffin8782 on Dec 09, 2020, 04:35 PM
Your image appears as it does not exist on my screen. Mind if you show me the image? If it is google drive, I have a external tool (https://sites.google.com/site/gdocs2direct/home). There might be a proper way to go around this problem. If it is a CC problem, you might as well create a issue on the github.
Title: http.websocket closing when receiving data?
Post by: forced on Dec 09, 2020, 04:55 PM
Quote from: QuickMuffin8782 on Dec 09, 2020, 04:35 PMYour image appears as it does not exist on my screen. Mind if you show me the image? If it is google drive, I have a external tool (https://sites.google.com/site/gdocs2direct/home). There might be a proper way to go around this problem. If it is a CC problem, you might as well create a issue on the github.

It's just a gyazo link

https://gyazo.com/1d2c1a80410f8efd61a067a5622b038e
Title: http.websocket closing when receiving data?
Post by: SquidDev on Dec 09, 2020, 06:08 PM
I'm fairly sure it's not correct to just send the bytes along the websocket like that - you need to implement a similar encoding protocol. I suspect the connection is being closed due to a malformed packet being received.


I'd recommend using an external library to implement the main websocket protocol, as it's pretty complex to implement yourself (https://www.nuget.org/packages/Microsoft.AspNetCore.WebSockets/ if you're using C#, though major most languages will have a package somewhere).
Title: http.websocket closing when receiving data?
Post by: QuickMuffin8782 on Dec 09, 2020, 06:45 PM
Quote from: forced on Dec 09, 2020, 04:55 PMIt's just a gyazo link

https://gyazo.com/1d2c1a80410f8efd61a067a5622b038e

The following link is restricted by the ContentKeeper Auth. Gyazo isn't allowed with users who use it like me. Maybe try using Google Drive and use the following link with the image block should make me able to view it using Safari.
Title: http.websocket closing when receiving data?
Post by: forced on Dec 11, 2020, 05:26 PM
Quote from: SquidDev on Dec 09, 2020, 06:08 PMI'm fairly sure it's not correct to just send the bytes along the websocket like that - you need to implement a similar encoding protocol. I suspect the connection is being closed due to a malformed packet being received.


I'd recommend using an external library to implement the main websocket protocol, as it's pretty complex to implement yourself (https://www.nuget.org/packages/Microsoft.AspNetCore.WebSockets/ if you're using C#, though major most languages will have a package somewhere).

Thanks for the help, that was exactly what I needed.