LoginSignup
2
1

More than 1 year has passed since last update.

Python と C++ で websocket 通信するメモ

Posted at

背景

Python と C++ のプロセス間で numpy データや画像などそこそこ大きめのデータを扱いたい...
ローカルネットワーク越しで別 PC に処理させたいとか.

websocket(バイナリデータ扱える) を使ってみます.

C++ client, server

server も client も, 組み込みやすい civetweb が楽そうです!

websocket 有効にするには, USE_WEBSOCKET を define する必要があります.
また, デフォルトでは SSL 有効(SSL ライブラリとのリンクが必要)なので, ローカル PC やローカルネットワークでの利用で wss 不要であれば NO_SSL も define しましょう.

Python 側

client は websocket-client がよいでしょうか. websocket モジュールとは異なりますので注意です!

websocketとwebsocket_clientの競合でうまくimportできないのを解決する
https://qiita.com/ikentoh/items/34b980da07bfc8abfd81

server なら websocket-server かしら.

Note

CivetWebSocketHandler では, close の要求があると, handleClose が呼ばれるまえに handleData の bits に OPCODE_CONNECTION_CLOSE が立って 2 bytes のデータが来るようです.

        virtual bool handleData(CivetServer *server,
                                struct mg_connection *conn,
                                int bits,
                                char *data,
                                size_t data_len) {

                if (bits & MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE) {
                  printf("WS close\n");
                  return 0;
                }
         ...

と判定するとよいでしょうか.

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1