-
Notifications
You must be signed in to change notification settings - Fork 0
/
internetManager.cs
115 lines (102 loc) · 3.86 KB
/
internetManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media;
namespace chatwindow
{
class internetManager
{
static ClientWebSocket websocks;
private const string connectionString = "ws://35.226.104.102:8080/chat?username={0}&opponent={1}";
static internetManager()
{
//websocks = new ClientWebSocket();
}
public static async Task<bool> connectAsync(string username, string opponent)
{
string thisConnectionString = String.Format(connectionString,username, opponent);
thisConnectionString = thisConnectionString.Replace("#", "%23");
Uri uuri = new Uri(thisConnectionString);
websocks = new ClientWebSocket();
try
{
//UriBuilder connstring = new UriBuilder("ws", "localhost", 8080, "ws");
await websocks.ConnectAsync(uuri, CancellationToken.None);
Console.WriteLine("Finished Connection");
}
catch (Exception ex)
{
Console.WriteLine("Shit");
Console.WriteLine($"ERROR - {ex.Message}");
}
//SShould do a single mesage to recieve here, but I am too lazy to code it
return true;
}
public static async Task Send(string data)
{
byte[] bytes = Encoding.ASCII.GetBytes(data);
ArraySegment<Byte> myArrSegAll = new ArraySegment<Byte>(bytes);
await websocks.SendAsync(myArrSegAll, WebSocketMessageType.Text, true, CancellationToken.None);
}
public static async Task Receive(ListBox listBox, CancellationToken ct)
{
var buffer = new ArraySegment<byte>(new byte[2048]);
do
{
//if websocks.
WebSocketReceiveResult result = null;
using (var ms = new MemoryStream())
{
do
{
if (ct.IsCancellationRequested)
{
return;
}
try
{
result = await websocks.ReceiveAsync(buffer, ct);
}catch(Exception e)
{
return;
}
ms.Write(buffer.Array, buffer.Offset, result.Count);
} while (!result.EndOfMessage);
if (result.MessageType == WebSocketMessageType.Close)
{
disconnect();
break;
}
ms.Seek(0, SeekOrigin.Begin);
using (var reader = new StreamReader(ms, Encoding.UTF8))
{
var message = await reader.ReadToEndAsync();
listBox.Items.Add(message);
if (VisualTreeHelper.GetChildrenCount(listBox) > 0)
{
Border border = (Border)VisualTreeHelper.GetChild(listBox, 0);
ScrollViewer scrollViewer = (ScrollViewer)VisualTreeHelper.GetChild(border, 0);
scrollViewer.ScrollToBottom();
}
}
}
} while (true);
}
public static void disconnect()
{
try
{
websocks.CloseAsync(WebSocketCloseStatus.NormalClosure, "Disconected", CancellationToken.None);
}catch(Exception e)
{
Console.WriteLine(e);
}
}
}
}