Statistics
| Branch: | Tag: | Revision:

mongoose / examples / websocket_chat_client / websocket_chat_client.c @ eaef5bd1

History | View | Annotate | Download (2.58 KB)

1
/*
2
 * Copyright (c) 2016 Cesanta Software Limited
3
 * All rights reserved
4
 */
5

    
6
/*
7
 * This is a WebSocket client example.
8
 * Prints WS frames to stdout and forwards stdin to server.
9
 */
10

    
11
#include "mongoose.h"
12

    
13
static int s_done = 0;
14
static int s_is_connected = 0;
15

    
16
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
17
  struct websocket_message *wm = (struct websocket_message *) ev_data;
18
  (void) nc;
19

    
20
  switch (ev) {
21
    case MG_EV_CONNECT: {
22
      int status = *((int *) ev_data);
23
      if (status != 0) {
24
        printf("-- Connection error: %d\n", status);
25
      }
26
      break;
27
    }
28
    case MG_EV_WEBSOCKET_HANDSHAKE_DONE: {
29
      printf("-- Connected\n");
30
      s_is_connected = 1;
31
      break;
32
    }
33
    case MG_EV_POLL: {
34
      char msg[500];
35
      int n = 0;
36
#ifdef _WIN32 /* Windows console input is special. */
37
      INPUT_RECORD inp[100];
38
      HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
39
      DWORD i, num;
40
      if (!PeekConsoleInput(h, inp, sizeof(inp) / sizeof(*inp), &num)) break;
41
      for (i = 0; i < num; i++) {
42
        if (inp[i].EventType == KEY_EVENT &&
43
            inp[i].Event.KeyEvent.wVirtualKeyCode == VK_RETURN) {
44
          break;
45
        }
46
      }
47
      if (i == num) break;
48
      if (!ReadConsole(h, msg, sizeof(msg), &num, NULL)) break;
49
      /* Un-unicode. This is totally not the right way to do it. */
50
      for (i = 0; i < num * 2; i += 2) msg[i / 2] = msg[i];
51
      n = (int) num;
52
#else /* For everybody else, we just read() stdin. */
53
      fd_set read_set, write_set, err_set;
54
      struct timeval timeout = {0, 0};
55
      FD_ZERO(&read_set);
56
      FD_ZERO(&write_set);
57
      FD_ZERO(&err_set);
58
      FD_SET(0 /* stdin */, &read_set);
59
      if (select(1, &read_set, &write_set, &err_set, &timeout) == 1) {
60
        n = read(0, msg, sizeof(msg));
61
      }
62
#endif
63
      if (n <= 0) break;
64
      while (msg[n - 1] == '\r' || msg[n - 1] == '\n') n--;
65
      mg_send_websocket_frame(nc, WEBSOCKET_OP_TEXT, msg, n);
66
      break;
67
    }
68
    case MG_EV_WEBSOCKET_FRAME: {
69
      printf("%.*s\n", (int) wm->size, wm->data);
70
      break;
71
    }
72
    case MG_EV_CLOSE: {
73
      if (s_is_connected) printf("-- Disconnected\n");
74
      s_done = 1;
75
      break;
76
    }
77
  }
78
}
79

    
80
int main(int argc, char **argv) {
81
  struct mg_mgr mgr;
82
  struct mg_connection *nc;
83
  const char *chat_server_url = argc > 1 ? argv[1] : "ws://127.0.0.1:8000";
84

    
85
  mg_mgr_init(&mgr, NULL);
86

    
87
  nc = mg_connect_ws(&mgr, ev_handler, chat_server_url, "ws_chat", NULL);
88
  if (nc == NULL) {
89
    fprintf(stderr, "Invalid address\n");
90
    return 1;
91
  }
92

    
93
  while (!s_done) {
94
    mg_mgr_poll(&mgr, 100);
95
  }
96
  mg_mgr_free(&mgr);
97

    
98
  return 0;
99
}