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
8
#include <emscripten.h>
13
void send_msg(ENetPeer *peer) {
14
/* Create a reliable packet of size 7 containing "packet\0" */
15
ENetPacket * packet = enet_packet_create ("packet",
16
strlen ("packet") + 1,
17
ENET_PACKET_FLAG_RELIABLE);
18
/* Extend the packet so and append the string "foo", so it now */
19
/* contains "packetfoo\0" */
20
enet_packet_resize (packet, strlen ("packetfoo") + 1);
21
strcpy (& packet -> data [strlen ("packet")], "foo");
22
/* Send the packet to the peer over channel id 0. */
23
/* One could also broadcast the packet by */
24
/* enet_host_broadcast (host, 0, packet); */
25
enet_peer_send (peer, 0, packet);
26
/* One could just use enet_host_service() instead. */
27
enet_host_flush (host);
31
static int counter = 0;
38
emscripten_cancel_main_loop();
44
//printf("enet host?\n");
45
if (enet_host_service (host, & event, 0) == 0) return;
46
printf("enet host, got event of type %d\n", event.type);
49
case ENET_EVENT_TYPE_CONNECT:
50
printf ("A new client connected from %x:%u.\n",
51
event.peer -> address.host,
52
event.peer -> address.port);
53
/* Store any relevant client information here. */
54
event.peer -> data = "Client information";
59
case ENET_EVENT_TYPE_RECEIVE:
60
printf ("A packet of length %u containing %s was received from %s on channel %u.\n",
61
event.packet -> dataLength,
65
/* Clean up the packet now that we're done using it. */
66
enet_packet_destroy (event.packet);
68
case ENET_EVENT_TYPE_DISCONNECT:
69
printf ("%s disconected.\n", event.peer -> data);
70
/* Reset the peer's client information. */
71
event.peer -> data = NULL;
72
enet_host_destroy(host);
75
printf("whaaa? %d\n", event.type);
79
int main (int argc, char ** argv)
81
if (enet_initialize () != 0)
83
fprintf (stderr, "An error occurred while initializing ENet.\n");
86
atexit (enet_deinitialize);
89
address.host = ENET_HOST_ANY;
92
host = enet_host_create (& address /* the address to bind the server host to */,
93
32 /* allow up to 32 clients and/or outgoing connections */,
94
2 /* allow up to 2 channels to be used, 0 and 1 */,
95
0 /* assume any amount of incoming bandwidth */,
96
0 /* assume any amount of outgoing bandwidth */);
100
"An error occurred while trying to create an ENet server host.\n");
105
emscripten_set_main_loop(main_loop, 3, 1);
107
while (1) main_loop();