当前位置:   article > 正文

cocos2d WebSocket简单例子_cocos websocket 单例

cocos websocket 单例
  1. #pragma once
  2. #include "cocos2d.h"
  3. #include "network/WebSocket.h"
  4. using namespace cocos2d;
  5. using namespace cocos2d::network;
  6. class TestWebSocketScene: public Layer ,WebSocket::Delegate
  7. {
  8. public:
  9. CREATE_FUNC(TestWebSocketScene);
  10. static Scene *createScene();
  11. bool init();
  12. virtual void onOpen(WebSocket* ws);
  13. virtual void onMessage(WebSocket* ws, const WebSocket::Data& data);
  14. virtual void onClose(WebSocket* ws);
  15. virtual void onError(WebSocket* ws, const WebSocket::ErrorCode& error);
  16. private:
  17. WebSocket *wsText;
  18. };

  1. #include "TestWebSocketScene.h"
  2. Scene *TestWebSocketScene::createScene()
  3. {
  4. Scene *s = Scene::create();
  5. auto l = TestWebSocketScene::create();
  6. s->addChild(l);
  7. return s;
  8. }
  9. bool TestWebSocketScene::init()
  10. {
  11. if (!Layer::init())return false;
  12. Size size = Director::getInstance()->getWinSize();
  13. wsText = nullptr;
  14. auto menu = Menu::create();
  15. this->addChild(menu);
  16. auto lblSendText = Label::create("init websocket text","Arial",22);
  17. auto menuSendText = MenuItemLabel::create(lblSendText, [=](Ref*sender)
  18. {
  19. wsText = new WebSocket();
  20. if (!wsText->init(*this, "ws://echo.websocket.org"))
  21. {
  22. CC_SAFE_DELETE(wsText);
  23. }
  24. });
  25. auto lalSend = Label::create("send text","Arial",22);
  26. auto menuSend = MenuItemLabel::create(lalSend, [=](Ref*)
  27. {
  28. if (wsText)
  29. {
  30. wsText->send("hello websocket");
  31. }
  32. });
  33. auto lalSendBinary = Label::create("send Binary", "Arial", 22);
  34. auto menuSendBinary = MenuItemLabel::create(lalSendBinary, [=](Ref*)
  35. {
  36. if (wsText)
  37. {
  38. char buffer[] = "hello \0 binary";
  39. wsText->send((unsigned char*)buffer,sizeof(buffer));
  40. }
  41. });
  42. menu->addChild(menuSendText);
  43. menu->addChild(menuSend);
  44. menu->addChild(menuSendBinary);
  45. menu->alignItemsVertically();
  46. return true;
  47. }
  48. void TestWebSocketScene::onOpen(WebSocket* ws)
  49. {
  50. log("WebSocket (%p) open",ws);
  51. if (ws&&wsText == ws)
  52. {
  53. log("WebSocket send text open");
  54. }
  55. }
  56. void TestWebSocketScene::onMessage(WebSocket* ws, const WebSocket::Data& data)
  57. {
  58. log("WebSocket (%p) onMessage", ws);
  59. if (!data.isBinary)
  60. {
  61. log("received %s",data.bytes);
  62. }
  63. else
  64. {
  65. std::string result;
  66. for (int i = 0; i < data.len; i++)
  67. {
  68. if (data.bytes[i] != '\0')
  69. {
  70. result += data.bytes[i];
  71. }
  72. else
  73. {
  74. result += "\'\\0'";
  75. }
  76. }
  77. log("recceived binary is :%s",result.c_str());
  78. }
  79. }
  80. void TestWebSocketScene::onClose(WebSocket* ws)
  81. {
  82. log("WebSocket (%p) onClose", ws);
  83. if (ws&&wsText == ws)
  84. {
  85. log("WebSocket send text onClose");
  86. }
  87. }
  88. void TestWebSocketScene::onError(WebSocket* ws, const WebSocket::ErrorCode& error)
  89. {
  90. log("WebSocket (%p) onError", ws);
  91. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/652376
推荐阅读
相关标签
  

闽ICP备14008679号