http.websocket closing when receiving data?

Started by forced, Dec 09, 2020, 03:46 PM

Previous topic - Next topic

forced

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?



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

QuickMuffin8782

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. 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.
GOTTA GO FAST!!!! - Sunrise Studios now calling testers! Check the post!
Gitlab - Join my official discord! - SS Discord

forced

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. 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

SquidDev

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).
GitHub | CC:Tweaked: A ComputerCraft fork | Plethora: A peripheral mod

QuickMuffin8782

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.
GOTTA GO FAST!!!! - Sunrise Studios now calling testers! Check the post!
Gitlab - Join my official discord! - SS Discord

forced

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.