Websocket学习

共5个方法:

//创建一个Socket实例

var ws = new WebSocket('ws://localhost:8080');

//打开Socket

ws.onopen = function(event) {

//发送一个初始化消息

ws.send('I am the client and I\'m listening!');

};

//监听消息

ws.onmessage = function(event) {

console.log('Client received a message', event);

};

//监听Socket的关闭

ws.onclose = function(event) {

console.log('Client notified socket has closed', event);

};

//连接发生错误

ws.onerror=function(){

console.log('Websocket error');

};

//关闭Socket....

//ws.close()


Websocket系列教程:

WebSocket(1)-- WebSocket API简介http://blog.csdn.net/yl02520/article/details/7296223

你可能感兴趣的:(Websocket学习)