diff --git a/README.md b/README.md index dd2cbf7..615ce73 100644 --- a/README.md +++ b/README.md @@ -12,50 +12,55 @@ bower install websocket npm install websocketjs ``` -### connect 连接websocket -ws.connect(url, callback, close, error) -```js -ws.connect('ws://127.0.0.1:8080',function(handlers,evn){ -//连接成功! -},function(handlers,evn){ -//关闭触发! - if(handlers.closeType === "kick"){ - console.log('这个是踢下线!的关闭!') - } -},function(handlers,evn){ -//连接失败! -}) -``` +## 测试 -### message 一个连接监听器 -//ws.message(callback) +进入 `test` 目录 在命令行中运行 `node sever.js` 会输出下面内容 -```js -ws.message(function(evn,handlers){ - console.log(evn.data,handlers) -}) ``` - -### send 通过Socket发送一条消息到服务器 -//ws.send() - -```js -ws.send('{}') //=>handlers +--WebSocket------------- +WebSocket address: ws://127.0.0.1:3001 +WebSocket has started. +--Server---------------- +Server address: http://127.0.0.1:8080 +Server running... press ctrl-c to stop. +Server has started. +------------------------ ``` -### 关闭连接的监听器 -//ws.disconnect(type,callback) 关闭类型 + +## 接口调用 ```js -ws.disconnect("kick",function(data){ +var socket = new ws('ws://127.0.0.1:3001'), + str = "JSLite.io"; -}) -``` +socket.onconnecting = function(evn){ + console.log("socket:onconnecting:",evn); + // sendMsg("wcj"); + +} +socket.onopen = function(evn){ + console.log("socket:onopen:",evn); + log('发了个消息!"'+str+'"'); + sendMsg(str); + +} +socket.onclose = function(evn){ + console.log("socket.onclose:",evn); + log('WebSocket 被你关闭了!,您老人家再也没有办法建立连接了?'); + +} +socket.onmessage = function(evn){ + console.log("socket:onmessage:",evn); + log('收到消息!"'+evn.data+'"'); + // socket.close() + +} -### 判断是websocket否连接 -//ws.isConnect() //=> bool +function sendMsg (str) { + console.log("socket:sendMsg:",socket); + socket.send(str); +} -```js -ws.isConnect() //=> true | false -``` +``` \ No newline at end of file