#include #include #include #include #define EXAMPLE_PORT 40000 // クライアントデータ構造体 struct ClientData { char userId[256]; }; static int callback_echo(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) { char *methodName; char *userId; char *responseData = NULL; // userのポインタを使用 struct ClientData *client_data = (struct ClientData *)user; switch (reason) { case LWS_CALLBACK_ESTABLISHED: { printf("Assigned Client\n"); break; } case LWS_CALLBACK_RECEIVE: { char *message = (char*)in; printf("Request message: %s\n", message); methodName = strtok(message, ","); // 登録済みか確認 if(strcmp(methodName, "SelReg") == 0) { userId = strtok(NULL, ","); // 42バイト文字列想定 char response[255]; int result = SelectRegist(userId, response); // 登録済みユーザ if(result == 0){ // ユーザIDを保持 strncpy(client_data->userId, userId, sizeof(client_data->userId) - 1); // エラー }else if (result == 1) { snprintf(response, sizeof(response), "1"); // 未登録ユーザ }else if (result == 2) { snprintf(response, sizeof(response), "2"); } printf("SelReg: %d\n", result); lws_write(wsi, (unsigned char*)response, strlen(response), LWS_WRITE_TEXT); } break; } default: break; } return 0; } static struct lws_protocols protocols[] = { { "echo-protocol", callback_echo, 0, 256, 0, NULL, 0 }, {NULL, NULL, 0, 0, 0, NULL, 0} }; // 登録済みかMySQLに問い合わせ(スタブ) int SelectRegist(char *user_id, char *query_result){ return 0; } int main() { struct lws_context_creation_info info; struct lws_context *context; struct lws_vhost *vhost; struct lws *wsi; int n = 0; int ret = 0; memset(&info, 0, sizeof(info)); info.port = EXAMPLE_PORT; info.protocols = protocols; context = lws_create_context(&info); if (context == NULL) { printf("Could not create libwebsocket context\n"); return 1; } printf("Starting server...\n"); while (n >= 0 && ret >= 0) { ret = lws_service(context, 0); } lws_context_destroy(context); return 0; }