项目作者: channels-frontend

项目描述 :
A convenience library to handle ASGI messages over websockets
高级语言: JavaScript
项目地址: git://github.com/channels-frontend/django-channels.git
创建时间: 2019-08-16T19:25:22Z
项目社区:https://github.com/channels-frontend/django-channels

开源协议:BSD 3-Clause "New" or "Revised" License

下载


Usage

Channels WebSocket wrapper.

Originally extracted from channels v2.1.3.

Note: This software is not endorsed by the Channels project.

To process messages:

  1. import { WebSocketBridge } from 'django-channels'
  2. const webSocketBridge = new WebSocketBridge();
  3. webSocketBridge.connect('/ws/');
  4. webSocketBridge.addEventListener("message", function(event) {
  5. console.log(event.data);
  6. });

To send messages:

  1. webSocketBridge.send({prop1: 'value1', prop2: 'value1'});

To demultiplex specific streams:

  1. const webSocketBridge = new WebSocketBridge();
  2. webSocketBridge.connect('/ws/');
  3. webSocketBridge.stream('mystream').addEventListener("message", function(event) {
  4. //`event.origin` will be the name of the stream
  5. console.log(event.data, event.origin);
  6. });
  7. webSocketBridge.stream('myotherstream').addEventListener("message", function(event) {
  8. console.info(event.data, event.origin);
  9. });

To send a message to a specific stream:

  1. webSocketBridge.stream('mystream').send({prop1: 'value1', prop2: 'value1'})

The WebSocketBridge instance exposes the underlaying ReconnectingWebSocket as the socket property. You can use this property to add any custom behavior. For example:

  1. webSocketBridge.socket.addEventListener('open', function() {
  2. console.log("Connected to WebSocket");
  3. })