testing stuff is in transpiler now
This commit is contained in:
		
							parent
							
								
									11faf235d5
								
							
						
					
					
						commit
						b04a243190
					
				|  | @ -1 +0,0 @@ | ||||||
| emcc -lwebsocket.js -o index.html test.c |  | ||||||
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
										
											Binary file not shown.
										
									
								
							|  | @ -1,131 +0,0 @@ | ||||||
| #include <stdio.h> |  | ||||||
| #include <stdlib.h> |  | ||||||
| #include <emscripten/websocket.h> |  | ||||||
| #include <assert.h> |  | ||||||
| 
 |  | ||||||
| // Vector3, 3 components
 |  | ||||||
| typedef struct Vector3 { |  | ||||||
|     float x;                // Vector x component
 |  | ||||||
|     float y;                // Vector y component
 |  | ||||||
|     float z;                // Vector z component
 |  | ||||||
| } Vector3; |  | ||||||
| 
 |  | ||||||
| // Camera, defines position/orientation in 3d space
 |  | ||||||
| typedef struct Camera3D { |  | ||||||
|     Vector3 position;       // Camera position
 |  | ||||||
|     Vector3 target;         // Camera target it looks-at
 |  | ||||||
|     Vector3 up;             // Camera up vector (rotation over its axis)
 |  | ||||||
|     float fovy;             // Camera field-of-view aperture in Y (degrees) in perspective, used as near plane width in orthographic
 |  | ||||||
|     int projection;         // Camera projection: CAMERA_PERSPECTIVE or CAMERA_ORTHOGRAPHIC
 |  | ||||||
| } Camera3D; |  | ||||||
| 
 |  | ||||||
| EM_BOOL WebSocketOpen(int eventType, const EmscriptenWebSocketOpenEvent *e, void *userData) { |  | ||||||
|   printf("open(eventType=%d, userData=%p)\n", eventType, userData); |  | ||||||
| 
 |  | ||||||
|   Camera3D camera = {0}; |  | ||||||
|   camera.position = (Vector3){-9.0f, 9.0f, 4.0f}; |  | ||||||
|   camera.target = (Vector3){9.0f, 9.0f, 0.0f}; |  | ||||||
|   camera.up = (Vector3){0.0f, 1.0f, 0.0f}; |  | ||||||
|   camera.fovy = 60.0f; |  | ||||||
|   camera.projection = 0; |  | ||||||
| 
 |  | ||||||
|   emscripten_websocket_send_binary(e->socket, &camera, sizeof(camera)); |  | ||||||
| 
 |  | ||||||
|   return 0; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| EM_BOOL WebSocketClose(int eventType, const EmscriptenWebSocketCloseEvent *e, void *userData) { |  | ||||||
|   printf("close(eventType=%d, wasClean=%d, code=%d, reason=%s, userData=%p)\n", eventType, e->wasClean, e->code, e->reason, userData); |  | ||||||
|   return 0; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| EM_BOOL WebSocketError(int eventType, const EmscriptenWebSocketErrorEvent *e, void *userData) { |  | ||||||
|   printf("error(eventType=%d, userData=%p)\n", eventType, userData); |  | ||||||
|   return 0; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| EM_BOOL WebSocketMessage(int eventType, const EmscriptenWebSocketMessageEvent *e, void *userData) { |  | ||||||
|   printf("message(eventType=%d, userData=%p data=%p, numBytes=%d, isText=%d)\n", eventType, userData, e->data, e->numBytes, e->isText); |  | ||||||
|   static int text_received = 0; |  | ||||||
|   if (e->isText) { |  | ||||||
|     printf("text data: \"%s\"\n", e->data); |  | ||||||
|     assert(strcmp((const char*)e->data, "hello on the other side") == 0); |  | ||||||
|     text_received = 1; |  | ||||||
|     return 0; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   Camera3D camera; |  | ||||||
|   memcpy(&camera, e->data, sizeof camera); |  | ||||||
| 
 |  | ||||||
|   printf("x: %f", camera.position.x); |  | ||||||
|   printf(" y: %f", camera.position.y); |  | ||||||
|   printf(" z: %f", camera.position.z); |  | ||||||
|   printf(" fovy: %f", camera.fovy); |  | ||||||
|   printf("\n"); |  | ||||||
| 
 |  | ||||||
|   emscripten_websocket_close(e->socket, 0, 0); |  | ||||||
|   emscripten_websocket_delete(e->socket); |  | ||||||
|   emscripten_force_exit(0); |  | ||||||
|   return 0; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| int main() { |  | ||||||
|   assert(emscripten_websocket_is_supported()); |  | ||||||
| 
 |  | ||||||
|   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); |  | ||||||
|   emscripten_exit_with_live_runtime(); |  | ||||||
|   return 0; |  | ||||||
| } |  | ||||||
|  | @ -1,101 +0,0 @@ | ||||||
| class Vector3 { |  | ||||||
|   constructor(init = {}, ptr = undefined) { |  | ||||||
|     this._size = 12; |  | ||||||
|     this._ptr = ptr.buffer || new ArrayBuffer(this._size); |  | ||||||
|     this._data = new DataView(this._ptr); |  | ||||||
|     for (const key of Object.keys(init)) { |  | ||||||
|       this[key] = init[key]; |  | ||||||
|     } |  | ||||||
|   } |  | ||||||
|   get x() { |  | ||||||
|     return this._data.getFloat32(0, true); |  | ||||||
|   } |  | ||||||
|   get y() { |  | ||||||
|     return this._data.getFloat32(4, true); |  | ||||||
|   } |  | ||||||
|   get z() { |  | ||||||
|     return this._data.getFloat32(8, true); |  | ||||||
|   } |  | ||||||
|   set x(v) { |  | ||||||
|     this._data.setFloat32(0, v, true); |  | ||||||
|   } |  | ||||||
|   set y(v) { |  | ||||||
|     this._data.setFloat32(4, v, true); |  | ||||||
|   } |  | ||||||
|   set z(v) { |  | ||||||
|     this._data.setFloat32(8, v, true); |  | ||||||
|   } |  | ||||||
|   get bytes() { |  | ||||||
|     return new Uint8Array(this._ptr); |  | ||||||
|   } |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| class Camera3D { |  | ||||||
|   constructor(init = {}, ptr = undefined) { |  | ||||||
|     this._size = 44; |  | ||||||
|     this._ptr = ptr.buffer || new ArrayBuffer(this._size); |  | ||||||
|     this._data = new DataView(this._ptr); |  | ||||||
|     for (const key of Object.keys(init)) { |  | ||||||
|       this[key] = init[key]; |  | ||||||
|     } |  | ||||||
|   } |  | ||||||
|   get position() { |  | ||||||
|     return new Vector3({}, new Uint8Array(this._ptr.slice(0, 12))); |  | ||||||
|   } |  | ||||||
|   get target() { |  | ||||||
|     return new Vector3({}, new Uint8Array(this._ptr.slice(12, 24))); |  | ||||||
|   } |  | ||||||
|   get up() { |  | ||||||
|     return new Vector3({}, new Uint8Array(this._ptr.slice(24, 36))); |  | ||||||
|   } |  | ||||||
|   get fovy() { |  | ||||||
|     return this._data.getFloat32(36, true); |  | ||||||
|   } |  | ||||||
|   get projection() { |  | ||||||
|     return this._data.getInt32(40, true); |  | ||||||
|   } |  | ||||||
|   set position(v) { |  | ||||||
|     this._data.set(v.bytes(), 0); |  | ||||||
|   } |  | ||||||
|   set target(v) { |  | ||||||
|     this._data.set(v.bytes(), 12); |  | ||||||
|   } |  | ||||||
|   set up(v) { |  | ||||||
|     this._data.set(v.bytes(), 24); |  | ||||||
|   } |  | ||||||
|   set fovy(v) { |  | ||||||
|     return this._data.setFloat32(36, v, true); |  | ||||||
|   } |  | ||||||
|   set projection(v) { |  | ||||||
|     return this._data.setInt32(40, v, true); |  | ||||||
|   } |  | ||||||
|   get bytes() { |  | ||||||
|     return new Uint8Array(this._ptr); |  | ||||||
|   } |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| var decoder = new TextDecoder("utf-8"); |  | ||||||
| var port = 8089; |  | ||||||
| var ws = require("ws"); |  | ||||||
| var wss = new ws.WebSocketServer({ port: port }); |  | ||||||
| console.log("WebSocket server listening on ws://localhost:" + port + "/"); |  | ||||||
| wss.on("connection", function (ws) { |  | ||||||
|   console.log("Client connected!"); |  | ||||||
|   ws.on("message", function (message, isBinary) { |  | ||||||
|     if (!isBinary) { |  | ||||||
|       var text = decoder.decode(new Uint8Array(message).buffer); |  | ||||||
|       console.log("received TEXT: " + text.length + " characters:"); |  | ||||||
|       console.log('  "' + text + '"'); |  | ||||||
|     } else { |  | ||||||
|       const camera = new Camera3D({}, new Uint8Array(message)); |  | ||||||
|       console.log(camera.position.x, camera.position.y, camera.position.z); |  | ||||||
|       console.log(camera.target.x, camera.target.y, camera.target.z); |  | ||||||
|       console.log(camera.up.x, camera.up.y, camera.up.z); |  | ||||||
|       console.log(camera.fovy); |  | ||||||
|       console.log(camera.projection); |  | ||||||
|       console.log(camera.bytes); |  | ||||||
| 
 |  | ||||||
|       ws.send(camera.bytes, { binary: true }); // Echo back the received message
 |  | ||||||
|     } |  | ||||||
|   }); |  | ||||||
| }); |  | ||||||
		Loading…
	
		Reference in New Issue