例子:
function main() {
var ws = new WebSocketX('ws://localhost:8888');
ws.on('open', onOpen).on('message', onMessage).on('error', onError).on('close', onClose);
}
function onOpen(ws) {
console.log('open');
ws.send('hello');
}
function onMessage(ws, msg) {
console.log('message:' + msg);
}
function onError() {
console.log('error');
}
function onClose() {
console.log('close');
}
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| uri | string | 必填 | uri,格式为:ws://localhost:8888 |
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| type | string | 必填 | 事件类型,支持:open,message,error,close |
| cb | function | 必填 | 处理事件的回调函数 |
| 参数名 | 类型 | 说明 |
|---|---|---|
| ws | WebSocket | WebSocket对象 |
| 参数名 | 类型 | 说明 |
|---|---|---|
| ws | WebSocket | WebSocket对象 |
| msg | String | 消息 |
| 参数名 | 类型 | 说明 |
|---|
| 参数名 | 类型 | 说明 |
|---|
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| msg | string | 必填 | 消息 |
| 参数名 | 类型 | 必填 | 说明 |
|---|