|
@@ -31,9 +31,11 @@ using namespace hv;
|
|
|
class MyContext {
|
|
class MyContext {
|
|
|
public:
|
|
public:
|
|
|
MyContext() {
|
|
MyContext() {
|
|
|
|
|
+ printf("MyContext::MyContext()\n");
|
|
|
timerID = INVALID_TIMER_ID;
|
|
timerID = INVALID_TIMER_ID;
|
|
|
}
|
|
}
|
|
|
~MyContext() {
|
|
~MyContext() {
|
|
|
|
|
+ printf("MyContext::~MyContext()\n");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
int handleMessage(const std::string& msg, enum ws_opcode opcode) {
|
|
int handleMessage(const std::string& msg, enum ws_opcode opcode) {
|
|
@@ -60,7 +62,7 @@ int main(int argc, char** argv) {
|
|
|
WebSocketService ws;
|
|
WebSocketService ws;
|
|
|
ws.onopen = [](const WebSocketChannelPtr& channel, const HttpRequestPtr& req) {
|
|
ws.onopen = [](const WebSocketChannelPtr& channel, const HttpRequestPtr& req) {
|
|
|
printf("onopen: GET %s\n", req->Path().c_str());
|
|
printf("onopen: GET %s\n", req->Path().c_str());
|
|
|
- MyContext* ctx = channel->newContext<MyContext>();
|
|
|
|
|
|
|
+ auto ctx = channel->newContextPtr<MyContext>();
|
|
|
// send(time) every 1s
|
|
// send(time) every 1s
|
|
|
ctx->timerID = setInterval(1000, [channel](TimerID id) {
|
|
ctx->timerID = setInterval(1000, [channel](TimerID id) {
|
|
|
if (channel->isConnected() && channel->isWriteComplete()) {
|
|
if (channel->isConnected() && channel->isWriteComplete()) {
|
|
@@ -72,16 +74,17 @@ int main(int argc, char** argv) {
|
|
|
});
|
|
});
|
|
|
};
|
|
};
|
|
|
ws.onmessage = [](const WebSocketChannelPtr& channel, const std::string& msg) {
|
|
ws.onmessage = [](const WebSocketChannelPtr& channel, const std::string& msg) {
|
|
|
- MyContext* ctx = channel->getContext<MyContext>();
|
|
|
|
|
|
|
+ auto ctx = channel->getContextPtr<MyContext>();
|
|
|
ctx->handleMessage(msg, channel->opcode);
|
|
ctx->handleMessage(msg, channel->opcode);
|
|
|
};
|
|
};
|
|
|
ws.onclose = [](const WebSocketChannelPtr& channel) {
|
|
ws.onclose = [](const WebSocketChannelPtr& channel) {
|
|
|
printf("onclose\n");
|
|
printf("onclose\n");
|
|
|
- MyContext* ctx = channel->getContext<MyContext>();
|
|
|
|
|
|
|
+ auto ctx = channel->getContextPtr<MyContext>();
|
|
|
if (ctx->timerID != INVALID_TIMER_ID) {
|
|
if (ctx->timerID != INVALID_TIMER_ID) {
|
|
|
killTimer(ctx->timerID);
|
|
killTimer(ctx->timerID);
|
|
|
|
|
+ ctx->timerID = INVALID_TIMER_ID;
|
|
|
}
|
|
}
|
|
|
- channel->deleteContext<MyContext>();
|
|
|
|
|
|
|
+ // channel->deleteContextPtr();
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
websocket_server_t server;
|
|
websocket_server_t server;
|