php websocket消息推送,php实现websocket实时消息推送

websocket

/**

0:未连接

1:连接成功,可通讯

2:正在关闭

3:连接已关闭或无法打开

*/

//创建一个webSocket 实例

var webSocket = new WebSocket("ws://192.168.31.152:8083");

webSocket.onerror = function (event){

onError(event);

};

// 打开websocket

webSocket.onopen = function (event){

onOpen(event);

};

//监听消息

webSocket.onmessage = function (event){

onMessage(event);

};

webSocket.onclose = function (event){

onClose(event);

}

//关闭监听websocket

function onError(event){

document.getElementById("msg").innerHTML = "

close

";

console.log("error"+event.data);

};

function onOpen(event){

console.log("open:"+sockState());

document.getElementById("msg").innerHTML = "

Connect to Service

";

};

function onMessage(event){

console.log("onMessage");

document.getElementById("msg").innerHTML += "

response:"+event.data+"

"

};

function onClose(event){

document.getElementById("msg").innerHTML = "

close

";

console.log("close:"+sockState());

webSocket.close();

}

function sockState(){

var status = ['未连接','连接成功,可通讯','正在关闭','连接已关闭或无法打开'];

return status[webSocket.readyState];

}

function start(event){

console.log(webSocket);

var msg = document.getElementById('text').value;

document.getElementById('text').value = '';

console.log("send:"+sockState());

console.log("msg="+msg);

webSocket.send("msg="+msg);

document.getElementById("msg").innerHTML += "

request"+msg+"

"

};

function close(event){

webSocket.close();

}

已标记关键词 清除标记
表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 1024 设计师:白松林 返回首页