linux编译运行build.sh,linux下libwebsockets编译及实例

最近想自己搭建一个webscoket协议的服务器,打算用libwebsockts这个库。

下载代码编译。

编写一个shell脚本

#!/bin/sh

# wget http://git.warmcat.com/cgi-bin/cgit/libwebsockets/snapshot/libwebsockets-1.4-chrome43-firefox-36.tar.gz

# tar xvzf libwebsockets-1.4-chrome43-firefox-36.tar.gz

#cd libwebsockets-1.4-chrome43-firefox-36

# build

git clone https://github.com/warmcat/libwebsockets.git

cd libwebsockets

documentRoot=`pwd`

mkdir build

cmake ..

make

执行shell脚本就下载编译成功了。

运行一个例子

其实在库里面有一些实例的.

cd build

#!/bin/sh

documentRoot=`pwd`

libdir=libwebsockets

cd $libdir/build/bin

./libwebsockets-test-server --resource_path=$documentRoot/test-server

这样就运行自带的服务器。运行结果.

0818b9ca8b590ca3270a3433284dd417.png

浏览器输入127.0.0.1:7681结果:

0818b9ca8b590ca3270a3433284dd417.png

这样就是完整的结果了。

搭建自己的websocket server

#include

#include

#include

#include

#include

#include

#define KGRN "\033[0;32;32m"

#define KCYN "\033[0;36m"

#define KRED "\033[0;32;31m"

#define KYEL "\033[1;33m"

#define KMAG "\033[0;35m"

#define KBLU "\033[0;32;34m"

#define KCYN_L "\033[1;36m"

#define RESET "\033[0m"

static int destroy_flag = 0;

static void INT_HANDLER(int signo) {

destroy_flag = 0;

}

/* *

* websocket_write_back: write the string data to the destination wsi.

*/

int websocket_write_back(struct lws *wsi_in, char *str, int str_size_in)

{

if (str == NULL || wsi_in == NULL)

return -1;

int n;

int len;

unsigned char *out = NULL;

if (str_size_in < 1)

len = strlen(str);

else

len = str_size_in;

out = (unsigned char *)malloc(sizeof(unsigned char)*(LWS_SEND_BUFFER_PRE_PADDING + len + LWS_SEND_BUFFER_POST_PADDING));

//* setup the buffer*/

memcpy (out + LWS_SEND_BUFFER_PRE_PADDING, str, len );

//* write out*/

n = lws_write(wsi_in, out + LWS_SEND_BUFFER_PRE_PADDING, len, LWS_WRITE_TEXT);

printf(KBLU"[websocket_write_back] %s\n"RESET, str);

//* free the buffer*/

free(out);

return n;

}

static int ws_service_callback(

struct lws *wsi,

enum lws_callback_reasons reason, void *user,

void *in, size_t len)

{

switch (reason) {

case LWS_CALLBACK_ESTABLISHED:

printf(KYEL"[Main Service] Connection established\n"RESET);

break;

//* If receive a data from client*/

case LWS_CALLBACK_RECEIVE:

printf(KCYN_L"[Main Service] Server recvived:%s\n"RESET,(char *)in);

//* echo back to client*/

websocket_write_back(wsi ,(char *)in, -1);

break;

case LWS_CALLBACK_CLOSED:

printf(KYEL"[Main Service] Client close.\n"RESET);

break;

default:

break;

}

return 0;

}

struct per_session_data {

int fd;

};

int main(void) {

// server url will usd port 5000

int port = 5000;

const char *interface = NULL;

struct lws_context_creation_info info;

struct lws_protocols protocol;

struct lws_context *context;

// Not using ssl

const char *cert_path = NULL;

const char *key_path = NULL;

// no special options

int opts = 0;

//* register the signal SIGINT handler */

struct sigaction act;

act.sa_handler = INT_HANDLER;

act.sa_flags = 0;

sigemptyset(&act.sa_mask);

sigaction( SIGINT, &act, 0);

//* setup websocket protocol */

protocol.name = "my-echo-protocol";

protocol.callback = ws_service_callback;

protocol.per_session_data_size=sizeof(struct per_session_data);

protocol.rx_buffer_size = 0;

//* setup websocket context info*/

memset(&info, 0, sizeof info);

info.port = port;

info.iface = interface;

info.protocols = &protocol;

info.extensions = lws_get_internal_extensions();

info.ssl_cert_filepath = cert_path;

info.ssl_private_key_filepath = key_path;

info.gid = -1;

info.uid = -1;

info.options = opts;

//* create libwebsocket context. */

context = lws_create_context(&info);

if (context == NULL) {

printf(KRED"[Main] Websocket context create error.\n"RESET);

return -1;

}

printf(KGRN"[Main] Websocket context create success.\n"RESET);

//* websocket service */

while ( !destroy_flag ) {

lws_service(context, 50);

}

usleep(10);

lws_context_destroy(context);

return 0;

}

执行脚本:

#!/bin/sh

libdir=libwebsockets

g++ -g -o ws_servertest_ws_server.cpp -I$libdir/lib -I$libdir/build -L$libdir/build/lib -lwebsockets

编写自己的websocket client

#include

#include

#include

#include

#include

#include

#include

#define KGRN "\033[0;32;32m"

#define KCYN "\033[0;36m"

#define KRED "\033[0;32;31m"

#define KYEL "\033[1;33m"

#define KBLU "\033[0;32;34m"

#define KCYN_L "\033[1;36m"

#define KBRN "\033[0;33m"

#define RESET "\033[0m"

static int destroy_flag = 0;

static int connection_flag = 0;

static int writeable_flag = 0;

static void INT_HANDLER(int signo) {

destroy_flag = 0;

}

struct session_data {

int fd;

};

struct pthread_routine_tool {

struct lws_context *context;

struct lws *wsi;

};

static int websocket_write_back(struct lws *wsi_in, char *str, int str_size_in)

{

if (str == NULL || wsi_in == NULL)

return -1;

int n;

int len;

unsigned char *out = NULL;

if (str_size_in < 1)

len = strlen(str);

else

len = str_size_in;

out = (unsigned char *)malloc(sizeof(unsigned char)*(LWS_SEND_BUFFER_PRE_PADDING + len + LWS_SEND_BUFFER_POST_PADDING));

//* setup the buffer*/

memcpy (out + LWS_SEND_BUFFER_PRE_PADDING, str, len );

//* write out*/

n = lws_write(wsi_in, out + LWS_SEND_BUFFER_PRE_PADDING, len, LWS_WRITE_TEXT);

printf(KBLU"[websocket_write_back] %s\n"RESET, str);

//* free the buffer*/

free(out);

return n;

}

static int ws_service_callback(

struct lws *wsi,

enum lws_callback_reasons reason, void *user,

void *in, size_t len)

{

switch (reason) {

case LWS_CALLBACK_CLIENT_ESTABLISHED:

printf(KYEL"[Main Service] Connect with server success.\n"RESET);

connection_flag = 1;

break;

case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:

printf(KRED"[Main Service] Connect with server error.\n"RESET);

destroy_flag = 1;

connection_flag = 0;

break;

case LWS_CALLBACK_CLOSED:

printf(KYEL"[Main Service] LWS_CALLBACK_CLOSED\n"RESET);

destroy_flag = 1;

connection_flag = 0;

break;

case LWS_CALLBACK_CLIENT_RECEIVE:

printf(KCYN_L"[Main Service] Client recvived:%s\n"RESET, (char *)in);

if (writeable_flag)

destroy_flag = 1;

break;

case LWS_CALLBACK_CLIENT_WRITEABLE :

printf(KYEL"[Main Service] On writeable is called. send byebye message\n"RESET);

websocket_write_back(wsi, "Byebye! See you later", -1);

writeable_flag = 1;

break;

default:

break;

}

return 0;

}

static void *pthread_routine(void *tool_in)

{

struct pthread_routine_tool *tool = (struct pthread_routine_tool*)tool_in;

printf(KBRN"[pthread_routine] Good day. This is pthread_routine.\n"RESET);

//* waiting for connection with server done.*/

while(!connection_flag)

usleep(1000*20);

//*Send greeting to server*/

printf(KBRN"[pthread_routine] Server is ready. send a greeting message to server.\n"RESET);

websocket_write_back(tool->wsi, "Good day", -1);

printf(KBRN"[pthread_routine] sleep 2 seconds then call onWritable\n"RESET);

sleep(1);

printf(KBRN"------------------------------------------------------\n"RESET);

sleep(1);

//printf(KBRN"[pthread_routine] sleep 2 seconds then call onWritable\n"RESET);

//*involked wriable*/

printf(KBRN"[pthread_routine] call on writable.\n"RESET);

lws_callback_on_writable(tool->wsi);

}

int main(void)

{

//* register the signal SIGINT handler */

struct sigaction act;

act.sa_handler = INT_HANDLER;

act.sa_flags = 0;

sigemptyset(&act.sa_mask);

sigaction( SIGINT, &act, 0);

struct lws_context *context = NULL;

struct lws_context_creation_info info;

struct lws *wsi = NULL;

struct lws_protocols protocol;

memset(&info, 0, sizeof info);

info.port = CONTEXT_PORT_NO_LISTEN;

info.iface = NULL;

info.protocols = &protocol;

info.ssl_cert_filepath = NULL;

info.ssl_private_key_filepath = NULL;

info.extensions = lws_get_internal_extensions();

info.gid = -1;

info.uid = -1;

info.options = 0;

protocol.name = "my-echo-protocol";

protocol.callback = &ws_service_callback;

protocol.per_session_data_size = sizeof(struct session_data);

protocol.rx_buffer_size = 0;

protocol.id = 0;

protocol.user = NULL;

context = lws_create_context(&info);

printf(KRED"[Main] context created.\n"RESET);

if (context == NULL) {

printf(KRED"[Main] context is NULL.\n"RESET);

return -1;

}

wsi = lws_client_connect(context, "localhost", 5000, 0, "/", "localhost:5000", NULL,

protocol.name, -1);

if (wsi == NULL) {

printf(KRED"[Main] wsi create error.\n"RESET);

return -1;

}

printf(KGRN"[Main] wsi create success.\n"RESET);

struct pthread_routine_tool tool;

tool.wsi = wsi;

tool.context = context;

pthread_t pid;

pthread_create(&pid, NULL, pthread_routine, &tool);

pthread_detach(pid);

while(!destroy_flag)

{

lws_service(context, 50);

}

lws_context_destroy(context);

return 0;

}编写脚本:

#!/bin/sh

libdir=libwebsockets

g++ -g -o ws_client test_ws_client.cpp -I$libdir/lib -I$libdir/build -L$libdir/build/lib -lwebsockets -lpthread

运行结果

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

红线部分是交互的结果.这样就是完整的一套逻辑了.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/257978.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Tomcat如何配置环境变量

1&#xff0c; JDK&#xff1a;版本为jdk-7-windows-i586.exe 下载地址: http://www.oracle.com/technetwork/java/javase/downloads/index.html 2&#xff0c;tomcat&#xff1a;版本为apache-tomcat-7.0.33-windows-x86.zip 下载地址&#xff1a;http://tomcat.apache.org/ 2…

eclipse常用快捷键——非常实用

1、eclipse 查看变量或方法被调用的快捷键如下&#xff1a; &#xff08;1&#xff09;双击选中变量或者方法&#xff08;2&#xff09;键盘上CtrlshiftG组合键 2、eclipse中查看接口实现类快捷键 先找到接口类打开,然后双击接口名选中,再按住ctrlT就可以了。 3、eclipse中全局…

反编译查看源码dex2jar

为什么80%的码农都做不了架构师&#xff1f;>>> 上次说到了用apktool反编译&#xff0c;这次我们来用dex2jar 把apk解压得到文件夹 文件夹打开看到这些文件 其中这个classes.dex就是这次需要用到的字节码文件 把这个字节码文件托到dex2jar目录里 命令行编辑 得到下…

linux命令验证sqlldr,Linux:sqlldr命令

第一步&#xff1a;写一个 ctl格式的控制文件CTL 控制文件的内容 &#xff1a;load data --1. 控制文件标识infilexxx.txt --2. 要导入的数据文件名insert into table test--3. 将文件插入到数据库的 test 表中fields terminated by X09 --4. 用于分割一行中各个属性值的符号(例…

STL 中的链表排序

一直以来学习排序算法&#xff0c; 都没有在链表排序上下太多功夫&#xff0c;因为用得不多。最近看STL源码&#xff0c;才发现&#xff0c;原来即使是链表&#xff0c;也能有时间复杂度为O(nlogn)的算法&#xff0c; 大大出乎我的意料之外&#xff0c;一般就能想到个插入排序。…

cmd更换编码类型

chcp 65001 UTF-8 65001 GBK 936 本文出自 “曾颐楠的播客” 博客&#xff0c;请务必保留此出处http://zengyinan.blog.51cto.com/9524976/1721475 转载于:https://www.cnblogs.com/zengyinanos/p/5042732.html

代码混淆之后定位线上bug

代码混淆的目的 代码混淆的目的是防止竞争对手通过反编译来阅读项目代码。 Android中通过ProGuard来做代码混淆&#xff08;当然也还有其他的产品可以做代码混淆&#xff09;。 bug日志反混淆 资料&#xff1a;错误log、mapping.txt 异常log&#xff1a; mapping.txt&#xff…

linux怎么切换不同版本的r,在linux中用同一个版本的R 同时安装 Seurat2 和 Seurat3

在linux中用同一个版本的R 同时安装 Seurat 2 和 Seurat 3Seurat 作为单细胞分析中的重量级R包&#xff0c;有多好用用&#xff0c;用过的人都知道。Seurat 分析流程基本涵盖了单细胞分析中的所有常见分析方法&#xff0c;包括filtering&#xff0c;tSNE&#xff0c;UMAP降维及…

Unity手游之路四3d旋转-四元数,欧拉角和变幻矩阵

http://blog.csdn.net/janeky/article/details/17272625 今天我们来谈谈关于Unity中的旋转。主要有三种方式。变换矩阵&#xff0c;四元数和欧拉角。 定义 变换矩阵可以执行任意的3d变换&#xff08;平移&#xff0c;旋转&#xff0c;缩放&#xff0c;切边&#xff09;并且透视…

本地通知

本地通知&#xff0c;local notification&#xff0c;用于基于时间行为的通知&#xff0c;比如有关日历或者todo列表的小应用。另外&#xff0c;应用如果在后台执行&#xff0c;iOS允许它在受限的时间内运行&#xff0c;它也会发现本地通知有用。比如&#xff0c;一个应用&…

Redux 并不慢,只是你使用姿势不对 —— 一份优化指南

原文地址&#xff1a;Redux 并不慢&#xff0c;只是你使用姿势不对 —— 一份优化指南原文作者&#xff1a;Julian Krispel译文出自&#xff1a;掘金翻译计划本文永久链接&#xff1a;github.com/xitu/gold-m…译者&#xff1a;reid3290校对者&#xff1a;sunui&#xff0c;xek…

把windows装到linux下,如何将WSL(Windows Subsystem for Linux 2)安装到Windows 10?

原标题&#xff1a;如何将WSL(Windows Subsystem for Linux 2)安装到Windows 10&#xff1f;Windows 10凭借大受欢迎的WSL(Windows Subsystem for Linux)进入Linux领域。由于最近推出了WSL的最新版WSL2&#xff0c;用户现在可以利用实际的Linux内核从Windows执行Linux任务。现在…

TWRP-recovery中文界面安装方法[转]

把下载到的ui.zip放入sdcard1/twrp文件夹。注意&#xff0c;是内置存储卡中。如没有上述文件夹&#xff0c;自行建立后通过文件管理器放入&#xff0c;不是卡刷。文件夹应如下所示&#xff1a;sdcard1&#xff08;内置SD&#xff09; &#xff5c; ┕--twrp&#xff08;文件夹…

如何定期备份网站数据

产生这个问题的背景是我在维护两个个人的网站&#xff0c;因为采用的是虚拟主机&#xff0c;有时候空间续费不及时等&#xff0c;都可能造成数据的丢失&#xff0c;为了保障数据不丢失&#xff0c;因为有必要每15天左右对网站数据进行备份以防止发生不当的事情。 我们希望做的就…

初创团队可能不适合应届生小孩

根据最近招聘中接触到的一些刚毕业小孩的表现&#xff0c;谈谈这个问题&#xff1a; 1、扛不住&#xff0c;初创团队一般最好一人撑一快工作&#xff0c;刚毕业经验比较薄的小孩在这方面一是心理上不敢担当&#xff0c;二是能力上确实还需要磨炼成长 2、初创团队的那个环境可能…

vba执行linux命令,从VBA中的shell命令捕获输出值?

慕盖茨4494581根据Andrew Lessard的回答&#xff0c;这是一个运行命令并将输出作为字符串返回的函数 -Public Function ShellRun(sCmd As String) As StringRun a shell command, returning the output as a stringDim oShell As ObjectSet oShell CreateObject("WScript…

溢出和剪裁,可见性

内容溢出和剪裁 如果一个元素的内容对于元素大小来说过大&#xff0c;就有可能溢出元素本身。对于此情况&#xff0c;有一些解决办法可选。 溢出 overflow 值 visible(默认):内容在元素框外可见。一般会导致内容超出其自己的元素框&#xff0c;但不会改变框的形状scroll:溢出部…

C#= 栈模仿堆的操作

//原理&#xff0c;利用两个栈&#xff0c;互相作用&#xff0c;来模仿堆的效果&#xff0c;先进先出。。 1 using System;2 using System.Collections.Generic;3 using System.Linq;4 using System.Threading.Tasks;5 6 namespace TwoStacksQueue7 {8 public class Progra…

linux计划任务执行日志,linux中centos制定计划任务执行命令并且输出日志

1.写脚本最简单的 写如下代码#!/bin/shABC1.每个命令之间用;隔开说明&#xff1a;各命令的执行给果&#xff0c;不会影响其它命令的执行。换句话说&#xff0c;各个命令都会执行&#xff0c;但不保证每个命令都执行成功。2.每个命令之间用&&隔开说明&#xff1a;若前面…

Java-大集合拆分为指定大小的小集合

因为Oracle数据的in 最大允许1000 ,超过就会报错&#xff0c; 所以需要将集合拆分为多个集合进行处理. /*** 拆分集合* param <T>* param resList 要拆分的集合* param count 每个集合的元素个数* return 返回拆分后的各个集合*/public static <T> List<L…