zoukankan      html  css  js  c++  java
  • WebSocket

    #ifndef __CC_WEBSOCKET_H__

    #define __CC_WEBSOCKET_H__


    #include "ExtensionMacros.h"

    #include "cocos2d.h"

    #include "libwebsockets.h"

    #include <list>


    NS_CC_EXT_BEGIN


    class WsThreadHelper;

    class WsMessage;


    class WebSocket

    {

    public:

        WebSocket();

        virtual ~WebSocket();

        

        /**

         *  @brief Data structure for message

         */

        struct Data

        {

            Data():bytes(NULL), len(0), isBinary(false){}

            char* bytes;

            int len;

            bool isBinary;

        };

        

        /**

         *  @brief Errors in websocket

         */

        enum ErrorCode

        {

            kErrorTimeout = 0,

            kErrorConnectionFailure,

            kErrorUnknown

        };


        /**

         *  @brief The delegate class to process websocket events.

         */

        class Delegate

        {

        public:

            virtual ~Delegate() {}

            virtual void onOpen(WebSocket* ws) = 0;

            virtual void onMessage(WebSocket* ws, const Data& data) = 0;

            virtual void onClose(WebSocket* ws) = 0;

            virtual void onError(WebSocket* ws, const ErrorCode& error) = 0;

        };

        

        

        /**

         *  @brief  The initialized method for websocket.

         *          It needs to be invoked right after websocket instance is allocated.

         *  @param  delegate The delegate which want to receive event from websocket.

         *  @param  url      The URL of websocket server.

         *  @return true: Success, false: Failure

         */

        bool init(const Delegate& delegate,

                  const std::string& url,

                  const std::vector<std::string>* protocols = NULL);

        

        /**

         *  @brief Sends string data to websocket server.

         */

        void send(const std::string& message);

        

        /**

         *  @brief Sends binary data to websocket server.

         */

        void send(const unsigned char* binaryMsg, unsigned int len);

        

        /**

         *  @brief Closes the connection to server.

         */

        void close();


        /**

         *  Websocket state

         */

        enum State

        {

            kStateConnecting = 0,

            kStateOpen,

            kStateClosing,

            kStateClosed

        };

        

        /**

         *  @brief Gets current state of connection.

         */

        State getReadyState();

    private:

        virtual void onSubThreadStarted();

        virtual int onSubThreadLoop();

        virtual void onSubThreadEnded();

        virtual void onUIThreadReceiveMessage(WsMessage* msg);

        


        friend class WebSocketCallbackWrapper;

        int onSocketCallback(struct libwebsocket_context *ctx,

                             struct libwebsocket *wsi,

                             enum libwebsocket_callback_reasons reason,

                             void *user, void *in, size_t len);

        

    private:

    State        _readyState;

        std::string  _host;

        unsigned int _port;

        std::string  _path;

        

        friend class WsThreadHelper;

        WsThreadHelper* _wsHelper;

        

        struct libwebsocket*         _wsInstance;

        struct libwebsocket_context* _wsContext;

        Delegate* _delegate;

        int _SSLConnection;

        struct libwebsocket_protocols* _wsProtocols;

    };


    NS_CC_EXT_END


    #endif /* defined(__CC_JSB_WEBSOCKET_H__) */


  • 相关阅读:
    5的阶乘以及任意输入一个数的阶乘
    继入门程序后的第一篇函数调用的小程序 比较两数大小
    计算机网络01-计算机网络与因特网
    2021春招冲刺-1227 数组去重 | 响应式布局 | 媒体查询 |浏览器帧
    2021春招冲刺-1225 TCP与UDP | 单例模式 | 回流与重绘
    2021春招冲刺-1223 进程线程的通信 | 字符串是否有效 | 数组转换与展平
    2021春招冲刺-1221 进程与线程的区别 | 进程的切换 | 单链表是否相交 | 元素水平/垂直居中的方式
    左边固定,右边自适应解决方案
    mock 模拟数据在框架中的简单使用
    一个页面从输入url到加载到内容,这个过程经历了什么
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/3771157.html
Copyright © 2011-2022 走看看