var webSocket = null;
window.onbeforeunload = function () {
//离开页面时的其他操作
};
/**
* 初始化websocket,建立连接
*/
if (!window.WebSocket) {
alert("您的浏览器不支持websocket");
return false;
}
webSocket = new WebSocket("ws://192.168.0.135:8080" +"/websocket/"+ "");
console.log("开始建立Socket");
// 收到服务端消息
var that = this
webSocket.onmessage = function (msg) {
var objMsg = JSON.parse(msg.data);
var socketMsg = {
account : that._account,
chat : that._chat,
}
for(var index in socketMsg){
if(objMsg.action == index) socketMsg[index](objMsg,that);
}
};
// 异常
webSocket.onerror = function (event) {
console.log("Socket异常",event);
};
// 建立连接
webSocket.onopen = function (event) {
console.log("Socket连接成功",event);
};
// 断线
webSocket.onclose = function (event) {
console.log("Socket断开",event);
}
window.onbeforeunload = function () {
//离开页面时的其他操作
};
/**
* 初始化websocket,建立连接
*/
if (!window.WebSocket) {
alert("您的浏览器不支持websocket");
return false;
}
webSocket = new WebSocket("ws://192.168.0.135:8080" +"/websocket/"+ "");
console.log("开始建立Socket");
// 收到服务端消息
var that = this
webSocket.onmessage = function (msg) {
var objMsg = JSON.parse(msg.data);
var socketMsg = {
account : that._account,
chat : that._chat,
}
for(var index in socketMsg){
if(objMsg.action == index) socketMsg[index](objMsg,that);
}
};
// 异常
webSocket.onerror = function (event) {
console.log("Socket异常",event);
};
// 建立连接
webSocket.onopen = function (event) {
console.log("Socket连接成功",event);
};
// 断线
webSocket.onclose = function (event) {
console.log("Socket断开",event);
}