forked from JKSunny/Quake3e
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathengine_snippet.c
More file actions
50 lines (42 loc) · 1.71 KB
/
engine_snippet.c
File metadata and controls
50 lines (42 loc) · 1.71 KB
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
/*
* TEMPLATE — not part of the CMake build.
* Copy into a client module, register a command in CL_Init (or similar), and call
* after WS_Init() (see cl_websocket.h / cl_websocket.c).
*/
#include "client.h"
#include "cl_websocket.h"
static wsHandle_t g_exampleWs = WS_INVALID_HANDLE;
static void ExampleWS_OnMessage( wsHandle_t h, wsOpcode_t op, const byte *data, int len ) {
(void)h;
(void)op;
Com_Printf( "WebSocket example: received %d byte(s)\n", len );
}
static void ExampleWS_OnOpen( wsHandle_t h ) {
Com_Printf( "WebSocket example: connected (handle %d)\n", h );
(void)WS_SendText( h, "hello from idtech3" );
}
static void ExampleWS_OnClose( wsHandle_t h, int code, const char *reason ) {
Com_Printf( "WebSocket example: closed code=%d %s\n", code, reason ? reason : "" );
if ( h == g_exampleWs ) {
g_exampleWs = WS_INVALID_HANDLE;
}
}
static void ExampleWS_OnError( wsHandle_t h, const char *err ) {
Com_Printf( S_COLOR_YELLOW "WebSocket example: %s (handle %d)\n", err ? err : "error", h );
}
static void ExampleWS_Connect_f( void ) {
if ( g_exampleWs != WS_INVALID_HANDLE ) {
WS_Disconnect( g_exampleWs );
}
/* Point at your local echo server (see examples/websocket/server/node) */
g_exampleWs = WS_Connect( "ws://127.0.0.1:8765/", ExampleWS_OnMessage, ExampleWS_OnOpen, ExampleWS_OnClose, ExampleWS_OnError );
}
static void ExampleWS_Disconnect_f( void ) {
if ( g_exampleWs != WS_INVALID_HANDLE ) {
WS_Disconnect( g_exampleWs );
g_exampleWs = WS_INVALID_HANDLE;
}
}
/* In your init: Cmd_AddCommand( "ws_example", ExampleWS_Connect_f ); */
/* Cmd_AddCommand( "ws_example_close", ExampleWS_Disconnect_f ); */
/* I/O: WS_Frame() is already called each client frame from cl_main.c. */