首页 > 代码库 > socket方法

socket方法

// 创建一个Socket实例
var socket = new WebSocket(‘ws://192.168.2.72:8430‘);

// 打开Socket
socket.onopen = function (event) {
//username,msg

// 发送一个初始化消息
socket.send(‘I am the client and I\‘m listening!‘);

// 监听消息
socket.onmessage = function (event) {
console.log(‘Client received a message‘, event);
};

// 监听Socket的关闭
socket.onclose = function (event) {
console.log(‘Client notified socket has closed‘, event);
};

// 关闭Socket....
//socket.close()
};

socket方法