-
-
Save fortizc/ba12b7520026d7138755 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <libwebsockets.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <signal.h> | |
struct _data | |
{ | |
int id; | |
char *nick; | |
char *msg; | |
}; | |
typedef struct _data chat_data; | |
// Connection id | |
static int id = 0; | |
static int chat_cb(struct libwebsocket_context *ctx, | |
struct libwebsocket *ws, | |
enum libwebsocket_callback_reasons reason, | |
void *user, void *in, size_t len) | |
{ | |
chat_data *data = (chat_data *)user; | |
char *msg = (char *) in; | |
switch (reason) | |
{ | |
case LWS_CALLBACK_ESTABLISHED: | |
printf("Connection Established\n"); | |
data->id = id++; | |
data->nick = (char *) malloc (sizeof(char) * 8); | |
sprintf(data->nick, "NO_NAME"); | |
data->msg = (char *)malloc(sizeof(char) * 16 + 4); | |
sprintf(data->msg, "Connection id = %d", data->id); | |
libwebsocket_callback_on_writable(ctx, ws); | |
break; | |
case LWS_CALLBACK_SERVER_WRITEABLE: | |
{ | |
// The 2 is for the ": " in the message | |
size_t size = strlen(data->msg) + strlen(data->nick) + 2; | |
size_t buffsize = LWS_SEND_BUFFER_PRE_PADDING | |
+ size | |
+ LWS_SEND_BUFFER_POST_PADDING; | |
unsigned char *buff = (unsigned char *)malloc(buffsize); | |
unsigned char *p = &buff[LWS_SEND_BUFFER_PRE_PADDING]; | |
sprintf((char *)p , "%s: %s", data->nick, data->msg); | |
libwebsocket_write(ws, p, size, LWS_WRITE_TEXT); | |
free(buff); | |
} | |
break; | |
case LWS_CALLBACK_RECEIVE: | |
if (!strcmp(&msg[len - 5], "@nick")) | |
{ | |
data->nick = (char *) realloc(data->nick, sizeof(char) * (len - 4)); | |
msg[len - 5] = 0; | |
memcpy(data->nick, msg, len - 4); | |
char *txt = (char *) malloc (sizeof(char) * 25 + strlen(data->nick)); | |
sprintf(txt, "Id %d now is know as %s\n\n", data->id, data->nick); | |
data->msg = txt; | |
libwebsocket_callback_on_writable(ctx, ws); | |
} | |
else | |
{ | |
data->msg = (char *)realloc(data->msg, (sizeof(char) * len) + 1); | |
memcpy(data->msg, msg, len + 1); | |
libwebsocket_callback_on_writable(ctx, ws); | |
} | |
break; | |
default: | |
break; | |
} | |
return 0; | |
} | |
static struct libwebsocket_protocols protocols[] = { | |
{ | |
"chat-protocol", | |
chat_cb, | |
sizeof(chat_data), | |
128 | |
}, | |
{ | |
NULL, NULL, 0 | |
} | |
}; | |
static volatile int end = 0; | |
void sig_cb(int sig) | |
{ | |
end = 1; | |
printf("\nStopping server...\n"); | |
} | |
int main() | |
{ | |
signal(SIGINT, sig_cb); | |
struct lws_context_creation_info info = { | |
9000, | |
NULL, | |
protocols, | |
libwebsocket_get_internal_extensions(), | |
0,0,0,0,0,0,0,0,0,0 | |
}; | |
struct libwebsocket_context *ctx; | |
ctx = libwebsocket_create_context(&info); | |
if (!ctx) | |
{ | |
fprintf(stderr, "Error, can not allocate context\n"); | |
return -1; | |
} | |
printf("Starting server...\n"); | |
while(!end) | |
libwebsocket_service(ctx, 50); | |
libwebsocket_context_destroy(ctx); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment