ZLMediaKit/tests/test_wsClient.cpp

71 lines
1.9 KiB
C++
Raw Normal View History

2019-09-16 17:45:27 +08:00
/*
2020-04-04 20:30:09 +08:00
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
2019-09-16 17:45:27 +08:00
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
2019-09-16 17:45:27 +08:00
*
2020-04-04 20:30:09 +08:00
* Use of this source code is governed by MIT license that can be found in the
* 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();
}
//被动断开连接回调
Push force replace (#2899) Co-authored-by: Alex <liyu7352@gmail.com> Co-authored-by: xia-chu <771730766@qq.com> Co-authored-by: Johnny <hellojinqiang@gmail.com> Co-authored-by: BackT0TheFuture <10088733+BackT0TheFuture@users.noreply.github.com> Co-authored-by: ljx0305 <ljx0305@gmail.com> Co-authored-by: Per-Arne Andersen <per@sysx.no> Co-authored-by: codeRATny <60806889+codeRATny@users.noreply.github.com> Co-authored-by: 老衲不出家 <tannzh2018@outlook.com> Co-authored-by: Kiki <haijuanchen.sun@gmail.com> Co-authored-by: PioLing <964472638@qq.com> Co-authored-by: dengjfzh <76604422+dengjfzh@users.noreply.github.com> Co-authored-by: a-ucontrol <55526028+a-ucontrol@users.noreply.github.com> Co-authored-by: 百鸣 <94030128+ixingqiao@users.noreply.github.com> Co-authored-by: fruit Juice <2317232721@qq.com> Co-authored-by: Luosh <fjlshyyyy@qq.com> Co-authored-by: tbago <moonzalor@gmail.com> Co-authored-by: Talus <xiaoxiaochenjian@gmail.com> Co-authored-by: 朱如洪 <zhu410289616@163.com> Co-authored-by: pedoc <pedoc@qq.com> Co-authored-by: XiaoYan Lin <linxiaoyan87@foxmail.com> Co-authored-by: xiangshengjye <46069012+xiangshengjye@users.noreply.github.com> Co-authored-by: tjpgt <602950305@qq.com> Co-authored-by: Nick <joyouswind@gmail.com> Co-authored-by: yogo-zhangyingzhe <100331270+yogo-zhangyingzhe@users.noreply.github.com> Co-authored-by: Xiaofeng Wang <wasphin@gmail.com> Co-authored-by: Dw9 <xweimvp@gmail.com> Co-authored-by: waken <33921191+mc373906408@users.noreply.github.com> Co-authored-by: Deepslient <1154547394@qq.com>
2023-10-12 17:09:20 +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秒触发一次该事件
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";
}
//连接服务器结果回调
void onConnect(const SockException &ex) override{
2023-04-23 00:10:18 +08:00
DebugL << ex;
2019-09-16 17:45:27 +08:00
}
//数据全部发送完毕后回调
void onFlush() override{
DebugL;
}
};
int main(int argc, char *argv[]) {
//设置退出信号处理函数
static semaphore sem;
signal(SIGINT, [](int) { sem.post(); });// 设置退出信号
//设置日志
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;
}