2014-11-21 25 views
1

我正在通過C websocket庫libwebsockets客戶端示例。C websocket庫,libwebsockets

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

Here就是這個例子,這個例子有兩個連接(代碼爲wsi_dumbwsi_mirror),這兩個連接都是我認爲的,我不知道第二個連接的目的是什麼。

使用第一個連接(在代碼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 
      */ 
     } 
    } 
} 

回答

0

我會混合起來,但在libwebsockets一個例子,你只打開一個瀏覽器(窗口或標籤),然後看你的鏡像第一個瀏覽器繪製線條和圓圈和發送到第二瀏覽器。