websocket服务器

绑定client端的ip和port,监听client的消息,如果对方发送一个json格式的字符串,就给对方回复一个json格式的"type-join"的字符串,实现交互

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#include <windows.h>
#include <libwebsockets.h>
#include <json-c/json.h>

#define MAX_JSON_SIZE 1024

static struct lws *client_conn = NULL;

static int callback_ws(struct lws *wsi, enum lws_callback_reasons reason,
                       void *user, void *in, size_t len)
{
    switch (reason) {
        case LWS_CALLBACK_CLIENT_ESTABLISHED:
            printf("WebSocket Client Connection established\n");
            client_conn = wsi;
            break;

        case LWS_CALLBACK_CLIENT_RECEIVE:
            printf("Received data from server: %s\n", (char *) in);
            // Parse JSON
            struct json_object *jobj, *jtype;
            jobj = json_tokener_parse(in);
            if (jobj == NULL) {
                printf("Error parsing JSON data\n");
                break;
            }
            // Get type field
            if (!json_object_object_get_ex(jobj, "type", &jtype)) {
                printf("JSON object does not contain type field\n");
                json_object_put(jobj);
                break;
            }
            const char *type = json_object_get_string(jtype);
            printf("Received JSON with type %s\n", type);
            // Check if type is join
            if (strcmp(type, "join") == 0) {
                // Send response JSON with type join
                char *response = "{\"type\": \"join\"}";
                lws_write(client_conn, (unsigned char *) response, strlen(response), LWS_WRITE_TEXT);
            }
            json_object_put(jobj);
            break;

        case LWS_CALLBACK_CLIENT_WRITEABLE:
            break;

        case LWS_CALLBACK_CLOSED:
            printf("WebSocket Connection Closed\n");
            lws_context_destroy(wsi->context);
            break;

        default:
            break;
    }

    return 0;
}

int main(int argc, char **argv)
{
    WSADATA wsa_data;
    int result = WSAStartup(MAKEWORD(2, 2), &wsa_data);
    if (result != 0) {
        printf("WSAStartup failed with error: %d\n", result);
        return -1;
    }

    const char *server_address = "192.168.1.100";
    const int server_port = 8080;

    // Initiate libwebsockets
    struct lws_context_creation_info info;
    memset(&info, 0, sizeof(info));
    info.port = CONTEXT_PORT_NO_LISTEN;
    info.protocols = (struct lws_protocols[]) {
        { "ws", callback_ws, 0 },
        { NULL, NULL, 0 }
    };

    struct lws_context *context = lws_create_context(&info);
    if (context == NULL) {
        printf("Error creating websocket context\n");
        return -1;
    }

    // Connect to server
    struct lws_client_connect_info connect_info;
    memset(&connect_info, 0, sizeof(connect_info));
    connect_info.context = context;
    connect_info.address = server_address;
    connect_info.port = server_port;
    connect_info.path = "/";
    connect_info.host = connect_info.address;
    connect_info.ietf_version_or_minus_one = -1;
    connect_info.ssl_connection = 0;
    connect_info.client_exts = NULL;
    connect_info.method = "GET";

    struct lws *conn = lws_client_connect_via_info(&connect_info);
    if (conn == NULL) {
        printf("Error connecting to server\n");
        return -1;
    }

    // Wait for connection to be established
    while (client_conn == NULL) {
        lws_service(context, 0);
    }

    printf("Connected to server\n");

    // Wait for data to be received
    while (true) {
        lws_service(context, 0);
    }

    lws_context_destroy(context);

    return 0;
}
相关推荐
conkl1 小时前
Apache网页优化实战指南 - 让网站加载速度提升
linux·运维·服务器·开发语言·阿里云·apache
huangyuchi.2 小时前
【Linux】初见,进程概念
linux·服务器·操作系统·进程·进程管理·pcb·fork
搬码临时工2 小时前
域名访问内网网站知多少?通过自定义域名访问本地Web 服务
服务器·网络·tcp/ip
五阿哥爱跳舞2 小时前
【环境配置】解决linux每次打开终端都需要source .bashrc文件的问题
linux·运维·服务器
网易独家音乐人Mike Zhou3 小时前
【Linux应用】Linux系统日志上报服务,以及thttpd的配置、发送函数
linux·运维·服务器·mcu·物联网·嵌入式·iot
~央千澈~4 小时前
WebSocket与XMPP:即时通讯技术的本质区别与选择逻辑优雅草卓伊凡|片翼|许贝贝
网络·websocket·网络协议
陶然同学5 小时前
从零开始:VMware上的Linux与Java开发环境配置
linux·运维·服务器·vmware·虚拟机
盛满暮色 风止何安5 小时前
BGP基础
运维·服务器·网络·网络协议·tcp/ip·网络安全·智能路由器
阿拉丁的梦5 小时前
ue5的blender4.1groom毛发插件v012安装和使用方法(排除了冲突错误)
linux·运维·服务器
老六ip加速器8 小时前
手机IP地址更换的影响与方法
网络协议·tcp/ip·智能手机