Code Monkey home page Code Monkey logo

Comments (6)

theturtle32 avatar theturtle32 commented on July 21, 2024

You didn't specify a subprotocol in your request from the client, but you are accepting the connection with a subprotocol on the server. Your client code should request the same subprotocol that you are specifying when you accept the request on the server:

Client:

new WebSocket('ws://127.0.0.1:8080/', 'your-protocol-here');

Server:

wsServer.on('request', function(request) {
    var connection = request.accept('your-protocol-here', request.origin);
    // More code here...
}

from websocket-node.

phihag avatar phihag commented on July 21, 2024

Oh, you're totally right. That works, thanks! However, clients should still not be able to crash the server, should they?

from websocket-node.

theturtle32 avatar theturtle32 commented on July 21, 2024

It's more that a coding error on the server caused the crash. Throwing an
error there is intentional, meant to get your attention that you missed
something in your code. It's the server's responsibility to verify that
the client requested a subprotocol that the server knows how to speak. If
you skip that verification step, you're asking for trouble. The example
code I posted is actually too lazy. You should be looping over the
contents of request.requestedProtocols to make sure that the client
requested a protocol that you want to establish a connection for.

According to the WebSocket specification, the client can request multiple
subprotocols, and the server can accept the connection with one of them, or
no subprotocol specified. That allows the client to request a number of
different protocols, for example, "my-great-protocol" and
"my-even-better-protocol", and let the server choose the best protocol that
it knows how to speak. The server also has the option of not specifying a
protocol when accepting the connection: pass null for the protocol when
calling request.accept().

Trying to accept the connection with a protocol that the client didn't
request in the first place is an error condition.

Brian

On Fri, Jan 11, 2013 at 1:17 AM, Philipp Hagemeister <
[email protected]> wrote:

Oh, you're totally right. That works, thanks! However, clients should
still not be able to crash the server, should they?


Reply to this email directly or view it on GitHubhttps://github.com//issues/76#issuecomment-12136781.

Brian McKelvey
C.E.O & Co-Founder
Worlize Inc.
www.worlize.com

from websocket-node.

phihag avatar phihag commented on July 21, 2024

So I guess the question then becomes what would be better example code. Maybe leave out the subprotocol in the accept call, or catch the exception and abort properly?

from websocket-node.

hikari-no-yume avatar hikari-no-yume commented on July 21, 2024

I used to catch exceptions but realised it's the completely wrong way to do it. Just manually check the subprotocol, and then only run accept if it was correct.

from websocket-node.

imvikaskohli avatar imvikaskohli commented on July 21, 2024

@theturtle32 Tx man you saved so much time of ours

from websocket-node.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.