Giter VIP home page Giter VIP logo

Comments (13)

tashaxing avatar tashaxing commented on August 18, 2024 2
  • 函数指针
  • 使用类作为回调类,然后在类里面定义回调函数
  • lambda
  • 仿函数
  • functional类型

这些都行

from cpphttpdemo.

tashaxing avatar tashaxing commented on August 18, 2024

有的,看55行和110行
先判断是否url然后判断post类型,接受前端参数然后handle

from cpphttpdemo.

Wall-ee avatar Wall-ee commented on August 18, 2024

有的,看55行和110行
先判断是否url然后判断post类型,接受前端参数然后handle

嗯 我在 http_server 看到了代码 。但是有个问题。 我想在 AddHandler 的对象里面编写。但是发现 AddHandler 里面没有

http_message *http_req

这个参数,所以就只能写在http_server的文件里面了。

from cpphttpdemo.

tashaxing avatar tashaxing commented on August 18, 2024

简单,把AddHandler做成回调函数对象,把http_message作为回调参数传出来,就可以handle了,需要一点编程的小技巧

from cpphttpdemo.

tashaxing avatar tashaxing commented on August 18, 2024

一般真正用在项目中的代码不是想这个demo这么写的,都是用回调

用回调函数把需要的参数和网络连接句柄传入传出,就可以把http通信组件和业务逻辑分割

from cpphttpdemo.

Wall-ee avatar Wall-ee commented on August 18, 2024

好的 我去试试~大佬能提供2-3行的代码一个小示例吗?感激不尽 我这里C++这一块不太熟悉……

from cpphttpdemo.

tashaxing avatar tashaxing commented on August 18, 2024
// sth like this
void HttpServer::HandleHttpEvent(mg_connection *connection, http_message *http_req, Callback callback)
{
    // use callback to handle the request
    callback(connection, http_req);
}

// write your callback outside
typedef void(mg_connection *connection, http_message *http_req) Callback;

// pass the this callback function to the http event handle function as function pointer
void MyCallback(mg_connection *connection, http_message *http_req)
{
// check http_req

// use connection to send response
}

from cpphttpdemo.

Wall-ee avatar Wall-ee commented on August 18, 2024

ok 另外还有个问题 就是 用 SendHttpRsp(connection, buffer.GetString()) 返回json的时候 中文会乱码。但是 在debug中 看 buffer 的是正常的
内存中的查看结果:

{
    "userInfo": {
        "wxid": "asdfadsfasdf",
        "userName": "阿拉拉了",
        "lalalal": "你好啊"
    }

cout 结果也正常

{
    "userInfo": {
        "wxid": "asdfadsfasdf",
        "userName": "阿拉拉了",
        "lalalal": "你好啊"
    }
}

但是 postman 接受的 返回json却编码不正常

{ "result": {
    "userInfo": {
        "wxid": "asdfadsfasdf",
        "userName": "��������",
        "lalalal": "����ү"
    }
} }

不知道怎么回事

from cpphttpdemo.

Wall-ee avatar Wall-ee commented on August 18, 2024

这样写 也不行:

SendHttpRsp(connection, "错误方法");

结果是这个:
{ "result": ���󷽷� }

from cpphttpdemo.

Wall-ee avatar Wall-ee commented on August 18, 2024

问题也定位了 就是 mongoose 这里的代码问题 但是我不知道怎么解决:

	//mg_printf_http_chunk(connection, "{ \"result\": %s }", rsp.c_str());
	mg_printf_http_chunk(connection, rsp.c_str());

from cpphttpdemo.

tashaxing avatar tashaxing commented on August 18, 2024

需要中文转码
试试这个,不是gbk就是utf8
https://blog.csdn.net/u012234115/article/details/83186386

from cpphttpdemo.

Wall-ee avatar Wall-ee commented on August 18, 2024

搞定 谢谢啦 windows 加一个 gbktoutf8 即可

from cpphttpdemo.

Wall-ee avatar Wall-ee commented on August 18, 2024

啊 post 的cilent 有么有示例?我想写一个发送的客户端~

from cpphttpdemo.

Related Issues (10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.