HTML5中WebSocket實現訊息推送的方法:
1、建立伺服器連線:
var socket = new WebSockect("ws://localhost:8080");
2、增加監控事件,當觸發open方法時,建立連線準備傳送訊息。
socket.addEventListener("open", function(){
console.log("Connection established, handle with event");
});
socket.onopen = function(){
console.log("Connection established, handle with function");
};
3、呼叫方法傳送訊息:
socket.send("Somme Message to send to the server");
4、關閉連線
socket.close();
HTML5中WebSocket實現訊息推送的方法:
1、建立伺服器連線:
var socket = new WebSockect("ws://localhost:8080");
2、增加監控事件,當觸發open方法時,建立連線準備傳送訊息。
socket.addEventListener("open", function(){
console.log("Connection established, handle with event");
});
socket.onopen = function(){
console.log("Connection established, handle with function");
};
3、呼叫方法傳送訊息:
socket.send("Somme Message to send to the server");
4、關閉連線
socket.close();