求助 感激 谢谢了 可以支付宝给款项奖励的

如题 我想把test cpp里面的websocket测试移植到一个新建的项目helloworld里
但是出现了如下的错误
引用了libnetwork和libextensions库
是不是还有需要引进的库
代码就是照搬test cpp的
只是把构成函数里面的语句移植到init里面了
非常感谢大家

cocos版本 ,运行编译环境?

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
//包含头文件
#include "network/WebSocket.h"

USING_NS_CC;
using namespace network;

//实现以下的接口
class HelloWorld : public Layer,public WebSocket::Delegate
{
public:
    static cocos2d::Scene* createScene();

    virtual bool init();  
    
    CREATE_FUNC(HelloWorld);
    
    //需要覆写以下四个方法
    void onOpen(WebSocket* ws);
    void onMessage(WebSocket* ws, const WebSocket::Data& data);
    void onClose(WebSocket* ws);
    void onError(WebSocket* ws, const WebSocket::ErrorCode& error);
    
private:
    WebSocket * _ws;
};

#endif // __HELLOWORLD_SCENE_H__



```

#include "HelloWorldScene.h"

USING_NS_CC;

Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();
    
    // 'layer' is an autorelease object
    auto layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    
    //新建一个websocket
    _ws = new WebSocket();
    
    //初始化websocket,第一个参数是delegate,需要传入一个引用,第二个是服务器地址,以下的服务器地址是WebSocket.org提供给测试用的
    _ws->init(*this,"ws://echo.websocket.org");
    
    //调用init连接服务器以后使用send来发送信息
    //发送文本信息
    _ws->send("hello websocket");
    //发送二进制信息
    unsigned char buf] = "hello websocket";
    _ws->send(buf,sizeof(buf));
    
    
    return true;
}

//调用init函数以后会连接服务器,如果连接成功就会回调onOpen函数
void HelloWorld::onOpen(WebSocket* ws)
{
    if(ws == _ws)
    {
        log("connect server succeed!");
    }
}

//当客户端收到服务器发送的数据以后调用onMessage函数
void HelloWorld::onMessage(WebSocket* ws, const WebSocket::Data& data)
{
    //如果是文本数据
    if(!data.isBinary)
    {
        log("data:%s,length:%ld",data.bytes,data.len);
    }
}

//当服务器和客户端断开连接的时候将调用onClose回调函数,这里需要释放内存
void HelloWorld::onClose(WebSocket* ws)
{
    if(ws == _ws)
    {
        CC_SAFE_DELETE(_ws);
    }
}

//客户端发送的请求如果发生错误,则会回调onError函数
void HelloWorld::onError(WebSocket* ws, const WebSocket::ErrorCode& error)
{
    if(ws == _ws)
    {
        if(WebSocket::ErrorCode::TIME_OUT == error)
            log("time out");
        else if(WebSocket::ErrorCode::CONNECTION_FAILURE == error)
            log("connection failure");
        else
            log("unknown");
    }
}


```

楼上是我写的,你看看能否解决你的问题。

非常感谢,我已看懂………………只是我不是楼主 :12:

不是楼主没关系,支付宝给钱就行 :877:

手贱点错了 :12: :12: :12:

windows7下各种编译不成功啊,要不要引入其他文件之类的呢?谢谢