Skip to content
ideawu edited this page Jul 8, 2015 · 42 revisions

iComet APIs

iComet 在系统中的角色

公共端口

客户端(浏览器, 手机app等)连接到公共端口, 接收服务器的推送. 默认的公共端口是 8100.

订阅频道

请求:

Long-polling

http://127.0.0.1:8100/poll?cname=$channel&seq=$next_seq&token=$token

Forever iframe

http://127.0.0.1:8100/iframe?cname=$channel&seq=$next_seq&token=$token

Streaming(HTTP endless chunk)

http://127.0.0.1:8100/stream?cname=$channel&seq=$next_seq&token=$token

Parameters:

  • cname: The name of the channel this client will subscribe to.
  • token: Token is returned by /sign.
  • seq: The sequence number of the next data message that will be sent by iComet server, the first seq is 1. iComet client must remember every message's sequence number, plus it with 1, and used for next polling or reconnect streaming.

响应:

如果HTTP响应的状态码不是 200, 一般是 iComet server 挂了.

Long-polling

icomet_cb([{type: "data", cname: "a", seq: "1", content: "a"}]);
icomet_cb({type: "data", cname: "a", seq: "1", content: "a"});

When there are buffered messages attached with the channel, the response is as the first one, the parameter of the function call is an array of message objects.

When a new message arrives, the response is as the second one, the parameter of the function call is the message object.

Since the response is received, the request is finished, so the client must send another request.

Forever iframe

<script>parent.icomet_cb({type: "data", cname: "a", seq: "0", content: "a"});</script>

Each message(old and new) is responsed as a HTTP chunk, the chunk is a snipet of JavaScript code, the request is not finished, so client do not need to send another request.

Streaming(HTTP endless chunk)

{type: "data", cname: "a", seq: "1", content: "a"}

Each message(old and new) is responsed as a HTTP chunk, the request is not finished, so client do not need to send another request.

Parameters:

  • type: 消息类型
    • data: 正常的消息.
    • next_seq: 当客户端收到这种类型的消息, 必须重置其序列号.
    • noop: 心跳消息.
    • 429: Error message, too many channels/subscribers.
    • 401: Error message, token error.

管理端口

管理端口是内部端口, 被内部的系统调用. 例如, 创建频道, 推送消息, 关闭频道, 等等. 默认的配置禁止客户端访问管理端口. 默认的管理端口是 8000l

/sign 创建频道

请求:

http://127.0.0.1:8000/sign?cname=$channel[&expires=60]

The created channel will be waiting for 60 seconds to expire before any subscriber arrives.

响应:

{type: "sign", cname: "a", seq: 0, token: "36289dcb55bc35aa6893f7557b7fc28c", expires: 30, sub_timeout: 10}

/push Push message

请求:

http://127.0.0.1:8000/push?cname=$channel&content=$content

响应:

{type: "ok"}

/push is an replacement of /pub, by using /push, the icomet server will escape any special char._ Just pass any string to the server by /push, icomet server will do the rest.

(Deprecated)/pub Publish/Push message

请求:

http://127.0.0.1:8000/pub?cname=$channel&content=$content

响应:

{type: "ok"}

content 必须是 json 编码的文本, 但不包括前面和后面的引号.

/close

请求:

http://127.0.0.1:8000/close?cname=$channel

响应:

/info

获取 comet-server 或者某个频道的信息.

请求:

http://127.0.0.1:8000/info
  • cname: 频道的名字(可选)

响应:

如果不指定 cname:

{"version": "0.2.2.1", "channels": 0, "subscribers": 0}

如果指定 cname:

{"cname": "a", "subscribers": 0}

/check

检查某个频道是否存在, 且有订阅者连接.

请求:

http://127.0.0.1:8000/check?cname=$channel

响应:

{"a": 1}
{}

/psub

Subscriber to comet-server's channel creation and deletion events, events are received as HTTP chunks.

请求:

http://127.0.0.1:8000/psub

响应:

1 channel
0 channel
Clone this wiki locally