Skip to content

Commit

Permalink
update websocket README
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed May 28, 2015
1 parent 4102ccf commit e13ca0a
Showing 1 changed file with 49 additions and 2 deletions.
51 changes: 49 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
## 下载

```
# bower 下载
bower install websocket
```
```
# npm 下载
npm install websocketjs
```

Expand All @@ -30,6 +30,53 @@ Server has started.


## 接口调用
将原生websocket调用 `socket = new WebSocket(url,protocol)` 更换成
`socket = new ws(url,protocol)`

```js
var socket,url='ws://127.0.0.1:3001';
//原生websocket使用换成下面 ws 方法
socket = new WebSocket(url);
//换成了ws 方法之后,下面的事件才有作用
socket = new ws(url);
```

### readyState
`readyState` 属性使用以下常数描述 `WebSocket` 的连线状态。

```js
socket.readyState //=>1
```

| 常数 || 描述 |
| -------- | -------- | -------- |
| CONNECTING | 0 | 连接尚未打开。|
| OPEN | 1 | 连接已打开,可以进行通讯。|
| CLOSING | 2 | 连接正在进行关闭程序。|
| CLOSED | 3 | 连接已关闭/连接不能打开。|

### onopen

连接成功会执行这个事件

### onmessage

接收websocket推送过来的消息

### onconnecting

这是个监听事件,当连接开始尝试进行,事件监听器被调用。

### onclose

websocket关闭会执行这个事件

### onclose

websocket关闭会执行这个事件


## 完整例子

```js
var socket = new ws('ws://127.0.0.1:3001'),
Expand Down

0 comments on commit e13ca0a

Please sign in to comment.