Skip to content

Latest commit

 

History

History
135 lines (110 loc) · 2.28 KB

websocket-apis.md

File metadata and controls

135 lines (110 loc) · 2.28 KB

Websocket API

Moco supports websocket api.

WARNING the json configuration below is just a snippet for one pair of request and response, instead of the whole configuration file.

Basis Usage

Websocket server is created from HTTP server.

HttpServer httpServer = httpServer(12306);
WebsocketServer server = httpServer.websocket("/ws");

If you want to response according to request content, Moco server can be configured as following:

server.request(by("foo")).response("bar");

You can also create websocket server in standalone api.

  • JSON
{
  "websocket": {
    "uri": "/ws",
    "sessions": [
      {
        "request": {
          "text": "foo"
        },
        "response": {
          "text": "bar"
        }
      }
    ]
  }
}

Connection Message

Client can receive Websocket server message while connection is established.

webSocketServer.connected("connected");
{
  "websocket": {
    "uri": "/ws",
    "connected": "connected"
  }
}

Ping/Pong

Ping/Pong is usually used as heartbeat in websocket.

websocketServer.ping("ping").pong("pong");
{
  "websocket": {
    "uri": "/ws",
    "pingpongs": [
        {
          "ping": "ping",
          "pong": "pong"
        }
    ]
  }
}

Broadcast

You can setup broadcast which means you broadcast your request to all client when you receive request.

server.request(by("foo")).response(broadcast("bar"));
{
  "request": {
    "text": "foo"
  },
  "response": {
    "broadcast": {
      "content": "bar"
    }
  }
}

If you just want to broadcast your message to some specific clients, group may help you. A client is supposed to join this group before your broadcast as following:

server.request(by("subscribe")).response(with("subscribed"), join(group("group")));
server.request(by("broadcast")).response(with("broadcasted"), broadcast("content", group("group")));
[
  {
    "request": {
      "text": "subscribe"
    },
    "response": {
      "content": "subscribed",
      "group": "group"
    }
  },
  {
    "request": {
      "text": "broadcast"
    },
    "response": {
      "content": "broadcasted",
      "broadcast": {
        "content": "content",
        "group": "group"
      }
    }
  }
]