WebSocket

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>WebSocket</title>
</head>
<body>
    
</body>
<script>
    var ids = 12;
    var ws = new WebSocket(‘ws://192.168.2.161:8080/webSocket?id=‘ + ids);
    // console.log(Socket)
    ws.onopen = function () {
        // 使用 send() 方法发送数据
        ws.send(‘发送数据‘)
        alert(‘数据发送中...‘)
    }

    //7接受服务端数据时触发事件
    ws.onmessage = function(evt) {
        var received_msg = evt.data;
        console.log(evt)
        console.log(‘数据已接受‘)
    }

    //断开 web socket 连接成功触发事件
    ws.onclose = function() {
        console.log(‘连接已关闭.....‘)
    }
</script>
</html>

相关推荐