| 95 | } |
| 96 | |
| 97 | EMSCRIPTEN_WEBSOCKET_T create_socket() { |
| 98 | EmscriptenWebSocketCreateAttributes attr; |
| 99 | emscripten_websocket_init_create_attributes(&attr); |
| 100 | |
| 101 | const char *url = "ws://localhost:8089/"; |
| 102 | attr.url = url; |
| 103 | // We don't really use a special protocol on the server backend in this test, |
| 104 | // but check that it can be passed. |
| 105 | attr.protocols = "binary,base64"; |
| 106 | |
| 107 | EMSCRIPTEN_WEBSOCKET_T socket = emscripten_websocket_new(&attr); |
| 108 | assert(socket >= 0); |
| 109 | |
| 110 | // URL: |
| 111 | int urlLength = 0; |
| 112 | EMSCRIPTEN_RESULT res = emscripten_websocket_get_url_length(socket, &urlLength); |
| 113 | assert(res == EMSCRIPTEN_RESULT_SUCCESS); |
| 114 | assert(urlLength == strlen(url)+1); |
| 115 | |
| 116 | char *url2 = malloc(urlLength); |
| 117 | res = emscripten_websocket_get_url(socket, url2, urlLength); |
| 118 | assert(res == EMSCRIPTEN_RESULT_SUCCESS); |
| 119 | printf("url: %s, verified: %s, length: %d\n", url, url2, urlLength); |
| 120 | assert(!strcmp(url, url2)); |
| 121 | |
| 122 | // Protocol: |
| 123 | int protocolLength = 0; |
| 124 | res = emscripten_websocket_get_protocol_length(socket, &protocolLength); |
| 125 | assert(res == EMSCRIPTEN_RESULT_SUCCESS); |
| 126 | assert(protocolLength == 1); // Null byte |
| 127 | |
| 128 | char *protocol = malloc(protocolLength); |
| 129 | res = emscripten_websocket_get_protocol(socket, protocol, protocolLength); |
| 130 | assert(res == EMSCRIPTEN_RESULT_SUCCESS); |
| 131 | // We don't really use a special protocol on the server backend in this test, |
| 132 | // but test that it comes out as an empty string at least. |
| 133 | assert(!strcmp(protocol, "")); |
| 134 | |
| 135 | // Extensions: |
| 136 | int extensionsLength = 0; |
| 137 | res = emscripten_websocket_get_extensions_length(socket, &extensionsLength); |
| 138 | assert(res == EMSCRIPTEN_RESULT_SUCCESS); |
| 139 | assert(extensionsLength == 1); // Null byte |
| 140 | |
| 141 | char *extensions = malloc(extensionsLength); |
| 142 | res = emscripten_websocket_get_extensions(socket, extensions, extensionsLength); |
| 143 | assert(res == EMSCRIPTEN_RESULT_SUCCESS); |
| 144 | // We don't really use any extensions on the server backend in this test, but |
| 145 | // test that it comes out as an empty string at least. |
| 146 | assert(!strcmp(extensions, "")); |
| 147 | |
| 148 | emscripten_websocket_set_onopen_callback(socket, (void*)0x42, WebSocketOpen); |
| 149 | emscripten_websocket_set_onclose_callback(socket, (void*)0x43, WebSocketClose); |
| 150 | emscripten_websocket_set_onerror_callback(socket, (void*)0x44, WebSocketError); |
| 151 | emscripten_websocket_set_onmessage_callback(socket, (void*)0x45, WebSocketMessage); |
| 152 | return socket; |
| 153 | } |
| 154 | |