Build a whole site using just websockets (via socket.io and node.js, no Ajax)?

前端 未结 4 1609
囚心锁ツ
囚心锁ツ 2021-02-08 07:39

Is this possible? Has anyone tried it?

Using websockets means there are no httpheaders being exchanged (like in a Ajax request) so there is definately a boost in speed o

相关标签:
4条回答
  • 2021-02-08 07:45

    It is definitely possible but I have not tried it. You will get a latency boost but the bandwidth boost will not be significant. The real problem is not going to be server resources (continuously polling the server via AJAX is likely harder on the server in most ways), but that AJAX has really solved a lot of the problems (especially the ones you will run into as your scope increases) so you will be rebuilding a lot of stuff for custom use.

    Unless you are actually running into a latency problem, I would suggest using standard AJAX. Or only use WebSockets for the part of you application that actually needs low latency so you are not recreating all the wheels.

    Yes, being able to have multiple clients connect to one listening port simultaneously is possible and done all the time (your web server almost certainly does so on port 80 for example). Your WebSocket server will have to handle the incoming connections properly (evented, threaded, or multi-process) but it's pretty much standard fair (google "socket programming YOUR_LANGUAGE").

    0 讨论(0)
  • 2021-02-08 07:51

    Have a look at SocketStream: https://github.com/socketstream/socketstream

    SocketStream is a new Node.js web framework dedicated to creating single-page real time websites.

    There will be some initial loading of resources but WebSockets will be used extensively for Client <-> Server communication.

    0 讨论(0)
  • 2021-02-08 07:57

    A late answer, but...

    I've recently rolled out my amusement web-app (jounce.space) that uses a websocket for everything except authentication. The websocket is used for both server-pushed data and client-initiated transactions.

    The site offers several different billiard-ball type amusements. Instead of a cue-stick, a player controls her ball with an elastic-band connecting her mouse to a ball. Unlike pool or billiards, you play continuously: you don't take turns.

    The elastic band solves (or minimizes) latency problems because the player naturally expects a delay when controlling via an elastic band. The server runs the game model, sending updated ball positions and game events every 40ms via websocket. Each client machine responds with its current mouse position.

    Besides channeling real-time game data, the socket also sends/recieves player arrivals & departures, game anoucements, and chat. The data are JSON formatted, e.g. every 40ms this is sent to each client:

    {
    
        "frameN": frame_sequence_num,
    
        "cpuLoad": frame_update_us/frameRate,
    
        "players":{
    
            <playerId>:[posX, posY, MouseX, MouseY],
    
            .
    
            .
    
            .
    
        }
    
    }
    

    Sometimes the frame data arrive out of order, so if frame_sequence_num is less than the previous one, that frame data is ignored. Sending everone's mouse position to all players lets everyone see each-other's elastic-band.

    Jounce.space is live as of this post. Visit and "view source" for more a more in depth look at the client side. Er, warning, the code isn't yet pretty; it's a hodge-podge of ad hocery, but it works well (except for MSIE). The server, written in golang, is hosted at openshift.redhat.com.

    0 讨论(0)
  • 2021-02-08 08:08

    Build a whole site using just websockets (via socket.io and node.js, no Ajax)? Is this possible? Has anyone tried it?

    I didn't hear of such a thing, but there are many websites that use Ajax intensively.

    Using websockets means there are no httpheaders being exchanged (like in a Ajax request) so there is definately a boost in speed of page display, however with the sockets you are holding a connection to the server even when nothing maybe happening, is this detrimental as number of users increase?

    First of all not all the browser support native websockets, just a few actually do so probably most of your users will have to do some long-pooling or something similar.

    with a connection being held between client and server, can the server still handle other clients connecting on the same port?

    You are right here, but I guess if you are using something like Node.js and don't have tens of thousands of users connected this won't be a problem.

    0 讨论(0)
提交回复
热议问题