Created
April 29, 2024 19:52
-
-
Save SnowCait/bee82eb104e7daf4edde6353e5e58db0 to your computer and use it in GitHub Desktop.
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
using System.Net.WebSockets; | |
using System.Text; | |
Uri uri = new("wss://yabu.me/"); | |
using ClientWebSocket ws = new(); | |
await ws.ConnectAsync(uri, default); | |
var req = "[\"REQ\", \"_\", {\"kinds\": [1], \"limit\": 10}]"; | |
Console.WriteLine("< " + req); | |
await ws.SendAsync(Encoding.UTF8.GetBytes(req), WebSocketMessageType.Text, true, default); | |
var buffer = new byte[1024]; | |
while (true) | |
{ | |
ArraySegment<byte> segment = new(buffer); | |
var result = await ws.ReceiveAsync(segment, default); | |
if (result.MessageType == WebSocketMessageType.Close) | |
{ | |
await ws.CloseAsync(WebSocketCloseStatus.NormalClosure, "OK", default); | |
return; | |
} | |
var count = result.Count; | |
while (!result.EndOfMessage) | |
{ | |
if (count >= buffer.Length) | |
{ | |
await ws.CloseAsync(WebSocketCloseStatus.NormalClosure, "That's too long", default); | |
return; | |
} | |
segment = new ArraySegment<byte>(buffer, count, buffer.Length - count); | |
result = await ws.ReceiveAsync(segment, default); | |
count += result.Count; | |
} | |
var message = Encoding.UTF8.GetString(buffer, 0, count); | |
Console.WriteLine("> " + message); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment