Created
June 12, 2024 16:33
-
-
Save Hakkadaikon/ce4777b32f43b2ff2fcaf88fdbb98c0d to your computer and use it in GitHub Desktop.
Nostr websocket client WinRT sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Windows.h> | |
#include <iostream> | |
#include <winrt/Windows.Foundation.h> | |
#include <winrt/Windows.Foundation.Collections.h> | |
#include <winrt/Windows.Web.Http.h> | |
#include <winrt/Windows.Networking.Sockets.h> | |
#include <winrt/Windows.Storage.Streams.h> | |
#include <winrt/Windows.Data.Json.h> | |
using namespace winrt; | |
using namespace Windows::Foundation; | |
using namespace Windows::Networking::Sockets; | |
using namespace Windows::Storage::Streams; | |
using namespace Windows::Web::Http; | |
using namespace Windows::Data::Json; | |
void parseEvent( | |
JsonObject& obj | |
) | |
{ | |
// Process as you like | |
auto id = obj.GetNamedString(L"id"); | |
auto pubkey = obj.GetNamedString(L"pubkey"); | |
auto created_at = obj.GetNamedNumber(L"created_at"); | |
auto kind = obj.GetNamedNumber(L"kind"); | |
auto content = obj.GetNamedString(L"content"); | |
auto sig = obj.GetNamedString(L"sig"); | |
auto tags = obj.GetNamedArray(L"tags"); | |
} | |
void devideEvent( | |
JsonArray& ja, | |
uint32_t& array_size, | |
const wchar_t* req_subid) | |
{ | |
if (array_size != 3) | |
{ | |
return; | |
} | |
auto subid = ja.GetAt(1).GetString(); | |
if (subid != req_subid) | |
{ | |
return; | |
} | |
auto event = ja.GetAt(2).GetObjectW(); | |
parseEvent(event); | |
} | |
void devideMessage(JsonArray& ja, const wchar_t* req_subid) | |
{ | |
auto array_size = ja.Size(); | |
if (array_size < 2) | |
{ | |
return; | |
} | |
auto kind = ja.GetAt(0).GetString(); | |
if (kind == L"EVENT") | |
{ | |
devideEvent(ja, array_size, req_subid); | |
} | |
} | |
void setWebsocketReceiveHandler( | |
MessageWebSocket& webSocket, | |
const wchar_t* req_subid | |
) | |
{ | |
// Set up the message received event handler | |
webSocket.MessageReceived( | |
[req_subid]( | |
MessageWebSocket const& sender, | |
MessageWebSocketMessageReceivedEventArgs const& args) | |
{ | |
DataReader reader = args.GetDataReader(); | |
uint32_t length = reader.UnconsumedBufferLength(); | |
reader.UnicodeEncoding(UnicodeEncoding::Utf8); | |
auto message = reader.ReadString(length); | |
auto ja = JsonArray::Parse(message); | |
devideMessage(ja, req_subid); | |
}); | |
} | |
void sendWebsocketMessage(DataWriter& writer, const wchar_t*& message) | |
{ | |
writer.WriteString(message); | |
writer.StoreAsync().get(); | |
std::wcout << L"Sent: " << message << std::endl; | |
} | |
int main() | |
{ | |
init_apartment(); | |
try | |
{ | |
MessageWebSocket webSocket; | |
const wchar_t* sub_id = L"_"; | |
Uri uri(L"wss://yabu.me"); | |
setWebsocketReceiveHandler(webSocket, sub_id); | |
// Connect to the server | |
webSocket.ConnectAsync(uri).get(); | |
// Send a test message | |
DataWriter writer(webSocket.OutputStream()); | |
auto testMessage = LR"(["REQ","_",{"kinds":[1],"limit":10}])"; | |
sendWebsocketMessage(writer, testMessage); | |
// Keep the console open to receive messages | |
std::wcout << L"Press Enter to exit...\n"; | |
std::wcin.get(); | |
// Close the WebSocket | |
webSocket.Close(1000, L"Normal closure"); | |
} | |
catch (const hresult_error& ex) | |
{ | |
std::wcerr << L"Error: " << ex.message().c_str() << std::endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment