spiral/websocket-client

Websocket JS 客户端

安装: 3

依赖: 0

建议者: 0

安全: 0

星星: 7

关注者: 12

分支: 0

语言:TypeScript

v0.0.4 2020-10-19 07:05 UTC

README

JavaScript WebSockets 客户端库,支持频道。

自从 RoadRunner 2.x 版本以来,通信协议已更改。以下为版本兼容性表。

安装

SFSocket 可通过 npm 或 yarn 安装

    npm install @spiralscout/websockets -D  
    yarn add @spiralscout/websockets 

然后可以这样使用

    import { SFSocket } from '@spiralscout/websockets';

或者通过包文件

    <script src="/build/socket.js"></script>
    <script type="text/javascript">
        var Socket = SFSocket.SFSocket;
        var connection = new Socket({ host: 'localhost'});
    </script>

如果您更喜欢使用 CDN,请使用以下 URL 获取最新版本

https://cdn.jsdelivr.net.cn/gh/spiral/websockets/build/socket.js

API

SFSocket 提供了使用 WebSockets 的简单方法

import { SFSocket } from '@spiralscout/websockets';

const socketOptions = { host: 'localhost' };

// create an instance of SFSocket
const ws = new SFSocket(socketOptions);

const prepareEvent = event => doSomething(event);

// subscribe to server
ws.subscribe('message', prepareEvent);

// runtime ready for all instances
SFSocket.ready();

// unsubscribe from server 
ws.unsubscribe('message', prepareEvent);

// disconnect from server 
ws.disconnect();

SFSocket

SFSocket 构造函数选项

SFSocket 支持标准(ws)和加密(wss)协议。

SFSocket 构造函数 new SFSocket(options: ISFSocketConfig) 期望 ISFSocketConfig 类型的选项

例如,要连接到 ws://some.domain.com/foo?bar=1,请使用以下代码

import { SFSocket } from '@spiralscout/websockets';

const socketOptions = {
  host: 'some.domain.com',
  port: '80',
  path: 'foo',
  queryParams: { bar: '1' }
}

const ws = new SFSocket(socketOptions);

SFSocket 频道

支持的事件

SFSocketChannel 允许订阅 connectedmessageclosederror 事件

SFSocket 还允许订阅 channel_joinedchannel_join_failedchannel_left 事件

ISFSocketEvent 结构

消息事件
const MessageEvent: ISFSocketEvent = {
  context: {
    channel: 'channel', // optional
    code: 1001, // optional
  },
  data: 'message',
  error: null,
  type: 'sfSocket:message',
};
错误事件
const ErrorEvent: ISFSocketEvent = {
  context: {
    channel: 'channel', // optional
    code: 1006, // optional
  },
  data: null,
  error: 'message',
  type: 'sfSocket:error',
};

示例

处理事件

const ws = new SFSocket(socketOptions);

ws.subscribe('connected', () => console.log('connected'));
ws.subscribe('error', (sfSocketEvent) => doSomething(sfSocketEvent));
ws.subscribe('message', (sfSocketEvent) => doSomething(sfSocketEvent));
ws.subscribe('closed', () => console.log('closed'));

const channel = ws.joinChannel('topic1');

channel.subscribe('connected', () => console.log('connected'));
channel.subscribe('error', (sfSocketEvent) => doSomething(sfSocketEvent));
channel.subscribe('message', (sfSocketEvent) => doSomething(sfSocketEvent));
channel.subscribe('closed', () => console.log('closed'));

创建多个频道

import { SFSocket } from '@spiralscout/websockets';

const socketOptions = { host: 'localhost' };

const ws = new SFSocket(socketOptions);

SFSocket.ready();

// create a channel and it is automatically connected to server
const channel1 = ws.joinChannel('channel_1');
const channel2 = ws.joinChannel('channel_2', true); // This one wont auto-join now

// subscribe the channel to server 
channel1.subscribe('message', (event) => doSomething(event));
channel2.subscribe('message', (event) => doSomething(event));

channel2.join(); // Start receiving messages for channel2 

// disconnect the channel from server 
channel1.leave();
channel2.leave();

// disconnect everything
ws.disconnect()

自定义命令

通过 sendCommand 方法支持发送自定义命令。不能将 joinleave 命令用作命令名称,有效载荷可以是任何可序列化的数据。

const cmd = 'foo'; // Any string except 'join' or 'leave'
const data = ['bar']; // Serializable data
ws.sendCommand(cmd, data);

开发

先决条件
Windows

在 windows 上执行 git config core.autocrlf false 来禁用自动换行符转换。