httpswift / swifter

Tiny http server engine written in Swift programming language.
BSD 3-Clause "New" or "Revised" License
3.89k stars 540 forks source link

Length of data sent to websocket #309

Open kimaldis opened 6 years ago

kimaldis commented 6 years ago

I'm using javascript websockets (Firefox) to send and receive text data to and from a swifter websocket:

    this.Websocket = new WebSocket("ws://" + window.location.host + "/ws")
    this.Websocket.send( "xxx" )

this all works just fine until the character count of data sent using Send() reaches 256. Anything over that and the socket disconnects. Above 256 only twenty or so characters reach the server. Below that everything arrives just fine.

I appreciate this probably isn't a Swifter problem but I'm a bit green on websockets and struggling. Any help much appreciated.

kimaldis commented 6 years ago

OK, I can reproduce this as below. Sending data over websocket to a server where length is >= 256 causes the connection to close, client error "The connection to ws://127.0.0.1:8084/ws was interrupted while the page was loading.".

The server recieves an empty string and gives the error "Protocol error: protocolError("Reserved frame bit has not been negocitated.")"

I've also tested the client code using ws://echo.websocket.org and it works OK, which points to swifter or something about swifter I'm missing. Any thoughts greatly appreciated.

thanks.

Swift:

    let WS = HttpServer()
    WS["/ws"] = websocket( { (session, text) in

        // gets empty string if something tries to send >= 256 length string here.
        print( "/WS got <\(text)>" )

    },{ (session, binary) in
        session.writeBinary(binary)
    })

    do {
        try WS.start( UInt16(8084) )
        print( "Webserver started on port \(8084)")
    } catch {
        print( "Error starting webserver: " + Errno.description())
    }

HTML:

<!doctype html>

<html lang="en">
    <head>
        <meta charset="utf-8">

            <title>Socket Test</title>
            <meta name="description" content="Socket Test">
            <meta name="author" content="Kim Aldis">

            <script>

            console.log("Start mkMessenger")
            var WS = new WebSocket("ws://127.0.0.1:8084/ws")        // fails
            //var WS = new WebSocket("ws://echo.websocket.org/")    // works

            WS.onopen = function(evt) {
                console.log("Opened ws")
                var string = ""
                for ( var i=0; i<256; i++ ) {       // 256 fails
                    string = string + "1"
                }
                WS.send( string )
            }
            WS.onclose = function(evt) {
                console.log("Closed Websocket", evt)
            };

            WS.onerror = function(evt) {
                console.log("Error in mkMessenger(): ", evt)
            };

            WS.onmessage = function(evt) {
                console.log( "msg", evt )
            }

            </script>

    </head>

    <body>
    test
    </body>
</html>