C websocket库,libwebsockets

时间:2014-11-21 13:19:39

标签: c websocket libwebsockets

我正在查看C websocket库libwebsockets客户端示例。

但我不明白其示例目的是什么。

Here就是一个例子,这个例子有两个连接(在代码wsi_dumbwsi_mirror中)我认为是相同的,我不知道第二个连接是什么#39;的目的是。

使用第一个连接(在代码wsi_dumb中),它似乎等待来自服务器libwebsocket_service()的请求然后......具有第二连接的内容(在代码wsi_mirror中) ?

以下是我所说的代码的一部分。

wsi_dumb = libwebsocket_client_connect(context, address, port, use_ssl,
            "/", argv[optind], argv[optind],
             protocols[PROTOCOL_DUMB_INCREMENT].name, ietf_version);

/*
 * sit there servicing the websocket context to handle incoming
 * packets, and drawing random circles on the mirror protocol websocket
 */

n = 0;
while (n >= 0 && !was_closed) {
    n = libwebsocket_service(context, 1000);

    if (wsi_mirror == NULL) {

        /* create a client websocket using mirror protocol */

        wsi_mirror = libwebsocket_client_connect(context, address, port,
             use_ssl,  "/", argv[optind], argv[optind],
                     protocols[PROTOCOL_LWS_MIRROR].name, ietf_version);

        mirror_lifetime = 10 + (random() & 1023);

        fprintf(stderr, "opened mirror connection with %d lifetime\n", mirror_lifetime);

    } else {

        mirror_lifetime--;
        if (mirror_lifetime == 0) {
            fprintf(stderr, "closing mirror session\n");
            libwebsocket_close_and_free_session(context,
                wsi_mirror, LWS_CLOSE_STATUS_GOINGAWAY);

            /*
             * wsi_mirror will get set to NULL in
             * callback when close completes
             */
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我可能会把它混淆但是libwebsockets中有一个例子,你只需打开第二个浏览器(窗口或标签),然后看到你在镜像的第一个浏览器中绘制的所有线条和圆圈,然后发送到第二个浏览器。