3.2.0 • Published 8 months ago

@zenweb/websocket v3.2.0

Weekly downloads
-
License
MIT
Repository
-
Last release
8 months ago

ZenWeb WebSocket module

ZenWeb

import { $debug, $query, Context, mapping } from 'zenweb';
import { WebSocket, RedisSocketSession } from '@zenweb/websocket';

export class Controller {
  @mapping()
  index(ctx: Context) {
    ctx.template('index.njk');
    return { name: 'Hello' };
  }

  @mapping()
  async ws(ws: WebSocket, ss: RedisSocketSession) {
    $debug('websocket 连接');
    const { name } = await $query.get({ name: '!trim1' });
    const room = 'chat';
    await ss.add(room, ws);
    ss.send(room, `欢迎 ${name} 来到聊天室!`);
    ws.once('close', () => {
      ss.send(room, `${name} 离开了聊天室!`);
    });
    ws.on('message', data => {
      const str = data.toString();
      ss.send(room, name + ': ' + str);
    });
  }
}
3.2.0

8 months ago

3.1.0

8 months ago

3.0.0

8 months ago

2.3.1

9 months ago

2.3.0

9 months ago

2.2.0

12 months ago

2.1.0

12 months ago

2.0.0

12 months ago

1.2.0

3 years ago

1.1.0

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago