goahead文档: https://www.embedthis.com/goahead/doc/users/goactions.html
websOpen(“web”, “route.txt”);
"web": 指定goahead的网页索引路径。
“route.txt”: 用来保存初始化路径配置(暂时没有用到,需要再研究)
注意:cgi url (/route/action) 默认一级route,一级action,当为/../../../多级别时,
可采用路径映射索引的方式
websSetIndex(“login.html”);
"login.html":
当访问http://127.0.0.1:8080时会默认加载名为login.html的网页,
默认为index.html。
websListen(“http://127.0.0.1:8080”)
"http://127.0.0.1:8080":
开启http服务,监听来自端口8080的消息
websListen(“https://127.0.0.1:443”)
"https://127.0.0.1:443":
开启https服务,监听来自端口443的消息
websAddRoute(“/”, “file”, -1)
只有调用这个才会加载文件
websAddRoute(“root”, “action”, -1)
"root": 添加http[s]://127.0.0.1/root/...索引
”action“: 是路径的属性
”-1“: 当为-1会自动计数,否则为0,1,2,3,4,5...
websDefineAction(“getip”, (void* test)(Webs* pWeb))
"getip": 添加一个名为getip的指令
"test":指令对应的接口函数,当调用http[s]://127.0.0.1/root/getip时会调用test函数
输入输出都通过pWeb参数传递
websWriteHeaders(pWeb, strlen(strJsonOut.c_str()), 0);
websWriteHeader(pWeb, "Content-Type", "%s", HTTP_CONTENT_TYPE);
websWriteHeader(pWeb, "Access-Control-Allow-Origin", "%s", "*");
websWriteHeader(pWeb, "Access-Control-Allow-Methods", "%s", "post");
websWriteHeader(pWeb, "Access-Control-Allow-Headers", "%s", "x-requested-with,content-type");
websWriteEndHeaders(pWeb);
websWrite(pWeb, "%s", "hello word");//content
websResponse(pWeb, 200, NULL);//码 200
websDone(pWeb);
int wait
websServiceEvents(&wait);
PUBLIC void websServiceEvents(int *finished)
{
WebsTime delay, nextEvent;
unsigned long dwThrID = 0;
#ifdef _MSC_VER
dwThrID = GetCurrentThreadId();
#else
dwThrID = pthread_self();
#endif
if (finished) {
*finished = 0;
}
delay = 0;
while (!finished || !*finished) {
trace(8, "[%lu] %s:%d tick %d, delay %d\n", dwThrID, __FUNCTION__, __LINE__, webGetTick(), delay);
if (socketSelect(-1, delay)) {///监听socket,更新websocket的currentEvent,阻塞超时为下一个定时器的超时时间
trace(8, "[%lu] %s:%d tick %d\n", dwThrID, __FUNCTION__, __LINE__, webGetTick());
socketProcess();///处理websocket的currentEvent
trace(8, "[%lu] %s:%d tick %d\n", dwThrID, __FUNCTION__, __LINE__, webGetTick());
}
trace(8, "[%lu] %s:%d tick %d\n", dwThrID, __FUNCTION__, __LINE__, webGetTick());
#if BIT_GOAHEAD_CGI && !BIT_ROM
delay = websCgiPoll();
trace(8, "[%lu] %s:%d tick %d, delay %d\n", dwThrID, __FUNCTION__, __LINE__, webGetTick(), delay);
#else
delay = MAXINT;
trace(8, "[%lu] %s:%d tick %d, delay %d\n", dwThrID, __FUNCTION__, __LINE__, webGetTick(), delay);
#endif
trace(8, "[%lu] %s:%d tick %d, delay %d\n", dwThrID, __FUNCTION__, __LINE__, webGetTick(), delay);
nextEvent = websRunEvents();///运行并刷新定时器
trace(8, "[%lu] %s:%d tick %d, nextEvent %d\n", dwThrID, __FUNCTION__, __LINE__, webGetTick(), nextEvent);
delay = min(delay, nextEvent);///根据下一个定时器的超时时间计算下次监听的超时时间
trace(8, "[%lu] %s:%d tick %d, delay %d\n", dwThrID, __FUNCTION__, __LINE__, webGetTick(), delay);
}
}
1:要开启https服务注意相关的编译宏,和上传key和安全证书
2:http默认端口8080,https默认端口为443
3:linux编译make -f goahead-linux-default.m