// 创建ws实例,建立连接(ws://121.40.165.18.8800 有广告) var ws = new WebSocket('wss://javascript.info/article/websocket/demo/hello') // 连接成功事件 ws.onopen = function(evt){ console.log('连接成功了---Connection open ...'); // 发送消息 ws.send('客户端发送消息---Hello WebSockets!') } // 接收消息事件 ws.onmessage = function(evt){ console.log('服务器接收消息---Received Message:'+evt.data); // 关闭连接 ws.close() } // 关闭连接事件 ws.onclose = function (evt){ console.log('关闭连接---Connection closed'); }
标签:基本,function,webSocket,log,---,ws,使用,evt,连接 From: https://www.cnblogs.com/Metx/p/16945431.html