首页 - 信息 - 用vs2010编译C2653_在VS2015下配置websocket++,用C++搭建一个简单的客户端——qq

用vs2010编译C2653_在VS2015下配置websocket++,用C++搭建一个简单的客户端——qq

2023-10-08 04:35

将之前编译好的OSG运行库解压到C:\,启动VS2010,编写一个简单的Win32控制台程序运行:

// TestOSG.cpp:定义控制台应用程序的入口点。 // #include #include #include "stdafx.h" int _tmain(int argc, _TCHAR* argv[]) { osgViewer::Viewer 查看器; viewer.setSceneData( osgDB::readNodeFile( "cow.osg" ) );返回www.genealogy-computer-tips.com(); }

按F7生成解,出现很多错误,提示:

1>----- 构建开始:项目:TestOSG,配置:调试 Win32 ------ 1>构建开始时间为 2015-4-29 19:18:27。 1>InitializeBuildStatus: 1> ?创建“Debug\TestOSG.unsuccessfulbuild”,因为指定了“AlwaysCreate”。 1>ClCompile: 1> ?所有输出都是最新的。 1> ?TestOSG.cpp 1>c:\documents and settings\administrator\my Documents\visual studio 2010\projects\testosg\testosg\testosg.cpp(3): warning C4627: “#include ”: 搜索跳过当使用预编译头时 1> ? ? ? ? ?向“StdAfx.h”添加指令或重新生成预编译头 1>c:\documents and settings\administrator\my Documents\visual studio 2010\projects\testosg \testosg\testosg.cpp(4): warning C4627: '#include ':在查找预编译头用法时跳过1>? ? ? ? ?添加指令到'StdAfx.h'或重新生成预编译头编译头1>c:\documents and settings\administrator\my Documents\visual studio 2010\projects\testosg\testosg\testosg.cpp(9): error C2653: 'osgViewer ': 不是类或命名空间名称 1>c: \documents and settings\administrator\my Documents\visual studio 2010\projects\testosg\testosg\testosg.cpp(9): 错误 C2065: 'Viewer': 未声明的标识符 1 >c:\documents and settings\administrator\ 我的文档ments\visual studio 2010\projects\testosg\testosg\testosg.cpp(9): 错误 C2146: 语法错误: 缺少“;” (在标识符“查看器”之前)1>c:\文档和设置\管理员\我的文档\ Visual Studio 2010 \项目\ testosg \ testosg \ testosg.cpp(9):错误C2065:'查看器':未声明的标识符1> c :\文档和设置\管理员\我的文档\ Visual Studio 2010 \项目\testosg\testosg\testosg.cpp(10):错误C2065:'查看器':未声明的标识符1> c:\文档和设置\管理员\我的文档\visual studio 2010\projects\testosg\testosg\testosg .cpp(10):错误C2228:“.setSceneData”左侧必须有一个类/结构/联合

等等。

折腾了好久才发现是

#include #include 需要与

位于相同位置

#include“stdafx.h”

改变

的位置,变成:

// TestOSG.cpp:定义控制台应用程序的入口点。 // #include "stdafx.h" #include #include int _tmain(int argc, _TCHAR* argv[]) { osgViewer::Viewer 查看器; viewer.setSceneData( osgDB::readNodeFile( "cow.osg" ) );返回www.genealogy-computer-tips.com(); }

就是这样!

简介

最近在做一个项目,需要在C++中通过WebSocket与服务器通信,但是我们在C++中不能直接使用WebSocket,所以在网上搜索后发现websocket++库非常合适。 Websocket基于HTTP协议,或者借用HTTP协议来完成部分握手,并且很好地支持长连接,比ajax轮询和长轮询更节省资源。关于WebSocket的具体知识,可以参考知乎上的这篇文章:https://www.genealogy-computer-tips.com/question/20215561

准备

去GitHub下载websocket++ https://www.genealogy-computer-tips.com/zaphoyd/websocketpp 设置好Boost后,直接去以下网站下载编译好的版本,无需自己编译(选择自己对应的ms版本,如VS2015 64位)选择boost_1_61_0-msvc-14.0-64.exe)https://www.genealogy-computer-tips.com/projects/boost/files/boost-binaries/1.61.0/

配置方法

第一步新建一个项目,并将项目属性调整为Release x64模式,如下图

第二步,打开项目-项目属性窗口,选择VC++目录,在“包含目录”下添加boost和websocket++目录,如下图

第三步,在“库目录”下添加boost的lib包目录

点击“应用”,然后点击“确定”,确定就完成了。

一个简单的WebSocket客户端

我们将下载的websocket++文件夹目录下的websocketpp-master\examples\echo_client中的C++代码文件复制到项目中测试运行。附上代码:

#包括

#包括

#包括

typedef websocketpp::client客户端;

使用 websocketpp::lib::占位符::_1;

使用 websocketpp::lib::占位符::_2;

使用 websocketpp::lib::bind;

//提取我们的配置发送的消息类型

typedef websocketpp::config::asio_client::message_type::ptr message_ptr;

// 每个传入消息都会调用此消息处理程序一次。是

//打印消息,然后将消息的副本发送回服务器。

void on_message(client* c, websocketpp::connection_hdl hdl, message_ptr msg) {

std::cout << "使用 hdl 调用 on_message: " << hdl.lock().get()

<< " and message: " << msg->get_payload()

<< std::endl;

websocketpp::lib::error_code ec;

c->发送(hdl, msg->get_payload(), msg->get_opcode(), ec);

如果 (ec) {

std::cout <<“回显失败,因为:” << ec.message() << std::endl;

}

}

int main(int argc, char* argv[]) {

// 创建客户端端点

客户端 c;

std::string uri = "ws://这里附上你自己的ws地址";

尝试{

// 将日志记录设置为非常详细(除了消息有效负载之外的所有内容)

c.set_access_channels(websocketpp::log::alevel::all);

c.clear_access_channels(websocketpp::log::alevel::frame_payload);

// 初始化ASIO

c.init_asio();

// 注册我们的消息处理程序

c.set_message_handler(bind(&on_message,&c,::_1,::_2));

websocketpp::lib::error_code ec;

client::connection_ptr con = c.get_connection(uri, ec);

如果 (ec) {

std::cout <<“无法创建连接,因为:” << ec.message() << std::endl;

返回0;

}

// 注意这里的connect只是请求连接。没有网络消息

// 交换,直到事件循环在下一行开始运行。

c.connect(con);

// 启动ASIO io_service运行循环

// 这将导致与服务器建立单个连接。 www.genealogy-computer-tips.com()

// 当此连接关闭时将退出。

www.genealogy-computer-tips.com();

} catch (websocketpp::Exception const & e) {

std::cout << e.what() << std::endl;

}

}

至此,我们就完成了websocket++的基本配置。

PS.如果编译器提示你“错误:后面有::的名称一定是类名或命名空间名”或者其他一些奇奇怪怪的错误,那多半是websocket++和boost的附加目录没有配置好

来源目录

? |-- stdafx.h

? |--Util目录

0条大师的评论