NodeJS创建WebSocket服务器

首先,安装ws模块:

- - --

代码:

- - - -----

- -
- --- - - ----- -- - ---

-------- --- -- - -
    - -
    - --------- - - -------- -----

    - -
    - -- - ---------------

    - -
    ----- - - -- ----
    ---------- -- -- - - - ---- ----

    - -
    ------- --- - -
        ----- -- ---------
    ---

    - -
    ------- -- - -
        ----- ---
    ---
---

----- - - - - ---