Skip to content
This repository has been archived by the owner on Jul 16, 2020. It is now read-only.

Commit

Permalink
fix: #71 [Xbox] Construct new websocket object on reconnect since web…
Browse files Browse the repository at this point in the history
…socket closure implies that that object is invalid.
  • Loading branch information
JoshSnider committed May 17, 2018
1 parent e989112 commit 5c48b0d
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions source/internal/winapp_websocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,31 +55,37 @@ class semaphore

class ws_client : public websocket
{
private: std::map<std::string, std::string> m_headers;
public:
ws_client() : m_closed(false)
{
m_ws = ref new MessageWebSocket();
m_ws->Control->MessageType = SocketMessageType::Utf8;
{
}

int add_header(const std::string& key, const std::string& value)
{
std::wstring keyWS = utf8_to_wstring(key);
std::wstring valueWS = utf8_to_wstring(value);
m_ws->SetRequestHeader(StringReference(keyWS.c_str()), StringReference(valueWS.c_str()));
m_headers[key] = value;
return 0;
}

int open(const std::string& uri, const on_ws_connect onConnect, const on_ws_message onMessage, const on_ws_error onError, const on_ws_close onClose)
{
int result = 0;
std::wstring uriWS = utf8_to_wstring(uri);
Uri^ uriRef = ref new Uri(StringReference(uriWS.c_str()));

bool connected = false;
unsigned short closeCode = 0;
std::string closeReason;

m_ws = ref new MessageWebSocket();
m_ws->Control->MessageType = SocketMessageType::Utf8;

// Add any headers.
for (auto header : m_headers)
{
std::wstring keyWS = utf8_to_wstring(header.first);
std::wstring valueWS = utf8_to_wstring(header.second);
m_ws->SetRequestHeader(StringReference(keyWS.c_str()), StringReference(valueWS.c_str()));
}

// Add closed and received handlers.
m_ws->Closed += ref new TypedEventHandler<IWebSocket^, WebSocketClosedEventArgs^>([&](IWebSocket^ sender, WebSocketClosedEventArgs^ args)
{
closeCode = args->Code;
Expand All @@ -102,6 +108,11 @@ class ws_client : public websocket
m_messagesSemaphore.notify();
}
});


// Connect asynchronously and then notify
std::wstring uriWS = utf8_to_wstring(uri);
Uri^ uriRef = ref new Uri(StringReference(uriWS.c_str()));

create_task(m_ws->ConnectAsync(uriRef)).then([&](task<void> prevTask)
{
Expand All @@ -119,6 +130,8 @@ class ws_client : public websocket
m_openSemaphore.notify();
});


// Wait for connection notificiiton.
m_openSemaphore.wait();
if (0 != result)
{
Expand Down

0 comments on commit 5c48b0d

Please sign in to comment.