File: test_websocket_send.c

package info (click to toggle)
emscripten 3.1.69%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 121,872 kB
  • sloc: ansic: 636,110; cpp: 425,974; javascript: 78,401; python: 58,404; sh: 49,154; pascal: 5,237; makefile: 3,365; asm: 2,415; lisp: 1,869
file content (131 lines) | stat: -rw-r--r-- 4,825 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <stdio.h>
#include <stdlib.h>
#include <emscripten/websocket.h>
#include <assert.h>

// This test performs that same server communications using two different
// sockets. This verifies that multiple sockets are supported simultaneously.
EMSCRIPTEN_WEBSOCKET_T sock1;
EMSCRIPTEN_WEBSOCKET_T sock2;

bool WebSocketOpen(int eventType, const EmscriptenWebSocketOpenEvent *e, void *userData) {
  printf("open(socket=%d, eventType=%d, userData=%p)\n", e->socket, eventType, userData);

  emscripten_websocket_send_utf8_text(e->socket, "hello on the other side");

  char data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  emscripten_websocket_send_binary(e->socket, data, sizeof(data));

  return 0;
}

bool WebSocketClose(int eventType, const EmscriptenWebSocketCloseEvent *e, void *userData) {
  printf("close(socket=%d, eventType=%d, wasClean=%d, code=%d, reason=%s, userData=%p)\n", e->socket, eventType, e->wasClean, e->code, e->reason, userData);
  assert(e->wasClean == 1);
  assert(e->code == 1005 /* No Status Rcvd */);
  emscripten_websocket_delete(e->socket);
  if (e->socket == sock1) {
    sock1 = 0;
  } else if (e->socket == sock2) {
    sock2 = 0;
  }
  // Once both sockets have been closed we are done.
  if (!sock1 && !sock2) {
    emscripten_force_exit(0);
  }
  return 0;
}

bool WebSocketError(int eventType, const EmscriptenWebSocketErrorEvent *e, void *userData) {
  printf("error(socket=%d, eventType=%d, userData=%p)\n", e->socket, eventType, userData);
  return 0;
}

bool WebSocketMessage(int eventType, const EmscriptenWebSocketMessageEvent *e, void *userData) {
  printf("message(socket=%d, eventType=%d, userData=%p data=%p, numBytes=%d, isText=%d)\n", e->socket, eventType, userData, e->data, e->numBytes, e->isText);
  static int text_received = 0;
  assert(e->socket == sock1 || e->socket == sock2);
  if (e->isText) {
    printf("text data: \"%s\"\n", e->data);
    assert(strcmp((const char*)e->data, "hello on the other side") == 0);
    text_received++;
    return 0;
  }

  // We expect to receive the text message before the binary one
  assert(text_received);
  printf("binary data:");
  for (int i = 0; i < e->numBytes; ++i) {
    printf(" %02X", e->data[i]);
    assert(e->data[i] == i);
  }
  printf("\n");
  emscripten_websocket_close(e->socket, 0, 0);
  return 0;
}

EMSCRIPTEN_WEBSOCKET_T create_socket() {
  EmscriptenWebSocketCreateAttributes attr;
  emscripten_websocket_init_create_attributes(&attr);

  const char *url = "ws://localhost:8089/";
  attr.url = url;
  // We don't really use a special protocol on the server backend in this test,
  // but check that it can be passed.
  attr.protocols = "binary,base64";

  EMSCRIPTEN_WEBSOCKET_T socket = emscripten_websocket_new(&attr);
  assert(socket >= 0);

  // URL:
  int urlLength = 0;
  EMSCRIPTEN_RESULT res = emscripten_websocket_get_url_length(socket, &urlLength);
  assert(res == EMSCRIPTEN_RESULT_SUCCESS);
  assert(urlLength == strlen(url)+1);

  char *url2 = malloc(urlLength);
  res = emscripten_websocket_get_url(socket, url2, urlLength);
  assert(res == EMSCRIPTEN_RESULT_SUCCESS);
  printf("url: %s, verified: %s, length: %d\n", url, url2, urlLength);
  assert(!strcmp(url, url2));

  // Protocol:
  int protocolLength = 0;
  res = emscripten_websocket_get_protocol_length(socket, &protocolLength);
  assert(res == EMSCRIPTEN_RESULT_SUCCESS);
  assert(protocolLength == 1); // Null byte

  char *protocol = malloc(protocolLength);
  res = emscripten_websocket_get_protocol(socket, protocol, protocolLength);
  assert(res == EMSCRIPTEN_RESULT_SUCCESS);
  // We don't really use a special protocol on the server backend in this test,
  // but test that it comes out as an empty string at least.
  assert(!strcmp(protocol, ""));

  // Extensions:
  int extensionsLength = 0;
  res = emscripten_websocket_get_extensions_length(socket, &extensionsLength);
  assert(res == EMSCRIPTEN_RESULT_SUCCESS);
  assert(extensionsLength == 1); // Null byte

  char *extensions = malloc(extensionsLength);
  res = emscripten_websocket_get_extensions(socket, extensions, extensionsLength);
  assert(res == EMSCRIPTEN_RESULT_SUCCESS);
  // We don't really use any extensions on the server backend in this test, but
  // test that it comes out as an empty string at least.
  assert(!strcmp(extensions, ""));

  emscripten_websocket_set_onopen_callback(socket, (void*)0x42, WebSocketOpen);
  emscripten_websocket_set_onclose_callback(socket, (void*)0x43, WebSocketClose);
  emscripten_websocket_set_onerror_callback(socket, (void*)0x44, WebSocketError);
  emscripten_websocket_set_onmessage_callback(socket, (void*)0x45, WebSocketMessage);
  return socket;
}

int main() {
  assert(emscripten_websocket_is_supported());
  sock1 = create_socket();
  sock2 = create_socket();
  emscripten_exit_with_live_runtime();
  return 0;
}