• 新建ws.html
    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. <title></title>
    6. </head>
    7. <body>
    8. <button type="button" id='send'>发送</button>
    9. <script type="text/javascript">
    10. var ws = new WebSocket("ws://192.168.10.59:8080/hub/uncertainty__bCJMJqJnBt");
    11. ws.onopen = (evt) => {
    12. console.log('连上')
    13. // ws.send('12313')
    14. }
    15. ws.onmessage = (evt) => {
    16. console.info('收到信息', evt.data)
    17. }
    18. ws.onerror = () => {
    19. console.info('出错饿了')
    20. }
    21. ws.onclose = () => {
    22. console.info('关闭了')
    23. }
    24. let btn = document.querySelector('#send')
    25. btn.onclick = () => {
    26. ws.send('{"a":"测不准","b":25,"pos":"China"}')
    27. }
    28. </script>
    29. </body>
    30. </html>
    • 链接的地址一定要有hub

    image.png
    在server.js中 有一个判断, 当两个ws都连接到服务端时, 既可以相互通信
    hub 后面连接是 房间名和 生成的generateId, 可以去掉不用在意