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

前端 未结 4 1618
囚心锁ツ
囚心锁ツ 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: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":{
    
            :[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.

提交回复
热议问题