1
// g++ -fpermissive ../enet_server.c -I/home/alon/Dev/emscripten/system/include/emscripten/ -Iinclude/ -fpermissive .libs/libenet.a -o enet_server ; g++ ../enet_client.c -I/home/alon/Dev/emscripten/system/include/emscripten/ -Iinclude/ -fpermissive .libs/libenet.a -o enet_client
4
#include <emscripten.h>
10
void send_msg(ENetPeer *peer) {
11
/* Create a reliable packet of size 7 containing "packet\0" */
12
ENetPacket * packet = enet_packet_create ("packet",
13
strlen ("packet") + 1,
14
ENET_PACKET_FLAG_RELIABLE);
15
/* Extend the packet so and append the string "foo", so it now */
16
/* contains "packetfoo\0" */
17
enet_packet_resize (packet, strlen ("packetfoo") + 1);
18
strcpy (& packet -> data [strlen ("packet")], "foo");
19
/* Send the packet to the peer over channel id 0. */
20
/* One could also broadcast the packet by */
21
/* enet_host_broadcast (host, 0, packet); */
22
enet_peer_send (peer, 0, packet);
23
/* One could just use enet_host_service() instead. */
24
enet_host_flush (host);
28
static int counter = 0;
34
emscripten_cancel_main_loop();
39
//printf("enet host?\n");
40
if (enet_host_service (host, & event, 0) == 0) return;
41
printf("enet host, got event of type %d\n", event.type);
44
case ENET_EVENT_TYPE_CONNECT:
45
printf ("A new client connected from %x:%u.\n",
46
event.peer -> address.host,
47
event.peer -> address.port);
48
/* Store any relevant client information here. */
49
event.peer -> data = "Client information";
54
case ENET_EVENT_TYPE_RECEIVE:
55
printf ("A packet of length %u containing %s was received from %s on channel %u.\n",
56
event.packet -> dataLength,
60
/* Clean up the packet now that we're done using it. */
61
enet_packet_destroy (event.packet);
63
case ENET_EVENT_TYPE_DISCONNECT:
64
printf ("%s disconected.\n", event.peer -> data);
65
/* Reset the peer's client information. */
66
event.peer -> data = NULL;
67
enet_host_destroy(host);
70
printf("whaaa? %d\n", event.type);
74
int main (int argc, char ** argv)
76
if (enet_initialize () != 0)
78
fprintf (stderr, "An error occurred while initializing ENet.\n");
81
atexit (enet_deinitialize);
84
address.host = ENET_HOST_ANY;
87
host = enet_host_create (& address /* the address to bind the server host to */,
88
32 /* allow up to 32 clients and/or outgoing connections */,
89
2 /* allow up to 2 channels to be used, 0 and 1 */,
90
0 /* assume any amount of incoming bandwidth */,
91
0 /* assume any amount of outgoing bandwidth */);
95
"An error occurred while trying to create an ENet server host.\n");
99
emscripten_set_main_loop(main_loop, 3, 1);