ZLMediaKit/tests/test_wsClient.cpp

77 lines
2.3 KiB
C++
Raw Normal View History

2019-09-16 17:45:27 +08:00
/*
2023-12-09 16:23:51 +08:00
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
2019-09-16 17:45:27 +08:00
*
2023-12-09 16:23:51 +08:00
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
2019-09-16 17:45:27 +08:00
*
2023-12-09 16:23:51 +08:00
* Use of this source code is governed by MIT-like license that can be found in the
2020-04-04 20:30:09 +08:00
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
2019-09-16 17:45:27 +08:00
*/
#include <signal.h>
#include <string>
#include <iostream>
#include "Util/MD5.h"
#include "Util/logger.h"
#include "Http/WebSocketClient.h"
2019-09-16 17:45:27 +08:00
using namespace std;
using namespace toolkit;
using namespace mediakit;
class EchoTcpClient : public TcpClient {
public:
EchoTcpClient(const EventPoller::Ptr &poller = nullptr){
InfoL;
}
~EchoTcpClient() override {
InfoL;
}
protected:
void onRecv(const Buffer::Ptr &pBuf) override {
DebugL << pBuf->toString();
}
// 被动断开连接回调 [AUTO-TRANSLATED:037fc69f]
// Passive disconnection callback
2023-04-28 22:03:16 +08:00
void onError(const SockException &ex) override {
2023-04-23 00:10:18 +08:00
WarnL << ex;
2019-09-16 17:45:27 +08:00
}
// tcp连接成功后每2秒触发一次该事件 [AUTO-TRANSLATED:4bcf65bf]
// Triggered every 2 seconds after a successful TCP connection
2019-09-16 17:45:27 +08:00
void onManager() override {
2020-04-23 17:50:12 +08:00
SockSender::send("echo test!");
2019-09-16 17:45:27 +08:00
DebugL << "send echo test";
}
// 连接服务器结果回调 [AUTO-TRANSLATED:b8306897]
// Server connection result callback
2019-09-16 17:45:27 +08:00
void onConnect(const SockException &ex) override{
2023-04-23 00:10:18 +08:00
DebugL << ex;
2019-09-16 17:45:27 +08:00
}
// 数据全部发送完毕后回调 [AUTO-TRANSLATED:f927c4c9]
// Callback after all data has been sent
2019-09-16 17:45:27 +08:00
void onFlush() override{
DebugL;
}
};
int main(int argc, char *argv[]) {
// 设置退出信号处理函数 [AUTO-TRANSLATED:4f047770]
// Set exit signal processing function
2019-09-16 17:45:27 +08:00
static semaphore sem;
signal(SIGINT, [](int) { sem.post(); });// 设置退出信号
// 设置日志 [AUTO-TRANSLATED:50372045]
// Set log
2019-09-16 17:45:27 +08:00
Logger::Instance().add(std::make_shared<ConsoleChannel>());
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
{
WebSocketClient<EchoTcpClient>::Ptr client = std::make_shared<WebSocketClient<EchoTcpClient> >();
client->startConnect("127.0.0.1", 80);
sem.wait();
}
2019-09-16 17:45:27 +08:00
return 0;
}