sourcey / libsourcey

C++14 evented IO libraries for high performance networking and media based applications
https://sourcey.com/libsourcey
GNU Lesser General Public License v2.1
1.31k stars 347 forks source link

What is correct way to create websocket client #92

Closed amitv87 closed 8 years ago

amitv87 commented 8 years ago

I tried following code with latest master, can someone please help me

#include <thread>
#include "scy/util.h"
#include "scy/http/client.h"
#include "scy/http/websocket.h"

using namespace std; using namespace scy; using namespace scy::http; using namespace scy::http::ws;

#define WS_URL "ws://echo.websocket.org:80"

class WSClient : public ws::ConnectionAdapter{
public:
  static WSClient* New(const char* url){
    ClientConnection::Ptr cconn = createConnection(URL(WS_URL));
    cconn->send();
    std::this_thread::sleep_for(std::chrono::milliseconds(5000));
    cout << "\nafter 5 seconds sleep\n" << endl;
    Connection* conn = (Connection*)(cconn.get());
    WSClient* wsc = nullptr;
    wsc = new WSClient(*conn, ClientSide);
    wsc->sendClientRequest();
    return wsc;
  }
private:
  WSClient(scy::http::Connection& con, scy::http::ws::Mode mode) :ConnectionAdapter(con, mode){

  }
  virtual void onSocketConnect() override{
    cout << "onSocketConnect" << endl;
  }
  virtual void onHandshakeComplete() override{
    cout << "onHandshakeComplete" << endl;
  }
};

int main(int argc, char const *argv[]){
  Logger::instance().add(new ConsoleChannel("debug", LTrace));
  WSClient* ws = WSClient::New(WS_URL);
  while(true) std::this_thread::sleep_for(std::chrono::milliseconds(2000000));
  return 0;
}

And this is the output I get

09:08:13 [debug] [scy::http::URL::URL()(36)] Parse char: ws://echo.websocket.org:80
09:08:13 [debug] [scy::http::URL::parse()(95)] Parsing: ws://echo.websocket.org:80
09:08:13 [trace] [scy::net::Socket::Socket()(37):0x288dfc] Create
09:08:13 [trace] [scy::net::TCPSocket::TCPSocket()(33):0x288da4] Create
09:08:13 [trace] [scy::net::TCPSocket::init()(49):0x288da4] Init
09:08:13 [trace] [scy::PacketStream::PacketStream()(37):0x298fb4] Create
09:08:13 [trace] [scy::PacketStream::PacketStream()(37):0x2990f4] Create
09:08:13 [trace] [scy::http::Connection::Connection()(43):0x298ef8] Create: 0x288dfc
09:08:13 [trace] [scy::http::ClientConnection::ClientConnection()(45):0x298ef8] Create: ws://echo.websocket.org:80
09:08:13 [trace] [scy::http::Parser::init()(74):0x299988] Init: 1
09:08:13 [trace] [scy::http::ConnectionAdapter::ConnectionAdapter()(297):0x2998c0] Create: 0x298ef8
09:08:13 [trace] [scy::http::Connection::replaceAdapter()(131):0x298ef8] Replace adapter: 0x2998c4
09:08:13 [trace] [scy::http::ws::WebSocketAdapter::WebSocketAdapter()(70):0x299df8] Create
09:08:13 [trace] [scy::http::Connection::replaceAdapter()(131):0x298ef8] Replace adapter: 0x299df8
09:08:13 [trace] [scy::http::Connection::replaceAdapter()(134):0x298ef8] Replace adapter: Delete existing: 0x2998c4
09:08:13 [trace] [scy::GarbageCollector::GarbageCollector()(39)] Create
09:08:13 [trace] [scy::http::ClientConnection::connect()(139):0x298ef8] Connecting
09:08:13 [trace] [scy::net::Socket::connect()(49):0x288dfc] Connect to host: echo.websocket.org:80

after 5 seconds sleep

09:08:18 [trace] [scy::http::ws::WebSocketAdapter::WebSocketAdapter()(70):0x29a660] Create
09:08:18 [trace] [scy::http::ws::WebSocketAdapter::sendClientRequest()(129):0x29a660] Client request: GET  HTTP/1.1
Connection: Upgrade
Host: echo.websocket.org
Sec-WebSocket-Key: YU1nS2dPQWxub01WQjZQQQ==
Sec-WebSocket-Version: 13
Upgrade: websocket

09:08:18 [trace] [scy::net::TCPSocket::send()(156):0x288da4] Send: 156
09:08:18 [warn] [scy::net::TCPSocket::send()(162)] Send error
amitv87 commented 8 years ago

Figured out a way, closing this.