libwebsockets(二)实现简易http服务器

根据官方的说明来看,从2.0版本起,http协议的服务器已经自动集成在库中,不需要我们自己去实现。下面介绍如何使用库去实现http服务器。

1、填充服务器创建需要的参数

lws_context_creation_info是创建服务器句柄时用来定制服务器信息的结构体,所以我们首先需要填充该结构体。该结构体的定义如下。

/*这里只列出我们常用的成员,注释很详细,不做过多解释*/
struct lws_context_creation_info {
    int port;
    /**< VHOST: Port to listen on. Use CONTEXT_PORT_NO_LISTEN to suppress
     * listening for a client. Use CONTEXT_PORT_NO_LISTEN_SERVER if you are
     * writing a server but you are using \ref sock-adopt instead of the
     * built-in listener */
    const char *iface;
    /**< VHOST: NULL to bind the listen socket to all interfaces, or the
     * interface name, eg, "eth2"
     * If options specifies LWS_SERVER_OPTION_UNIX_SOCK, this member is
     * the pathname of a UNIX domain socket. you can use the UNIX domain
     * sockets in abstract namespace, by prepending an at symbol to the
     * socket name. */
    const struct lws_protocols *protocols;
    /**< VHOST: Array of structures listing supported protocols and a protocol-
     * specific callback for each one.  The list is ended with an
     * entry that has a NULL callback pointer. */
    const struct lws_extension *extensions;
    /**< VHOST: NULL or array of lws_extension structs listing the
     * extensions this context supports. */
    const char *log_filepath;
    /**< VHOST: filepath to append logs to... this is opened before
     *      any dropping of initial privileges */
    const struct lws_http_mount *mounts;
    /**< VHOST: optional linked list of mounts for this vhost */

        ...
};

protocols用来指明该服务器可以处理的协议类型,以数组的形式提供并NULL作为数组结尾,如果不指定则默认使用http协议。

mounts用来设置与http服务器相关的参数,比如主机路径、URL路径、默认的主页文件等等。我们在这里初始化如下:


                
  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值