-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGameSenseClient.cs
89 lines (72 loc) · 3.05 KB
/
GameSenseClient.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;
using GameSenseNativeMessaging.Enums;
using GameSenseNativeMessaging.Events;
namespace GameSenseNativeMessaging
{
public class GameSenseClient
{
string Destination = "http://localhost:49682"; //Temp for now, will need to read this from : C:\ProgramData\SteelSeries\SteelSeries Engine 3\coreProps.json later.
public string Name = "CHROMECONNECT";
string DisplayName = "Chrome Connect";
IconColour Colour = IconColour.Yellow;
public void Register()
{
//First we register the application
var metaData = new GameRegistration(Name, DisplayName, Colour);
PostData(Destination + "/game_metadata", JsonConvert.DeserializeObject<JObject>(JsonConvert.SerializeObject(metaData)));
}
public void RegisterEvent(EventRegistration gameEvent)
{
gameEvent.ApplicationName = Name;
var jObject = JsonConvert.DeserializeObject<JObject>(JsonConvert.SerializeObject(gameEvent));
PostData(Destination + "/register_game_event", jObject);
}
public void UnRegister()
{
var metaData = new GameRegistration(Name, DisplayName, Colour);
PostData(Destination + "/remove_game", JsonConvert.DeserializeObject<JObject>(JsonConvert.SerializeObject(metaData)));
}
public void UnRegisterEvent(EventRegistration gameEvent)
{
gameEvent.ApplicationName = Name;
var jObject = JsonConvert.DeserializeObject<JObject>(JsonConvert.SerializeObject(gameEvent));
PostData(Destination + "/remove_game_event", jObject);
}
public void SendEvent(GameEvent gameEvent)
{
gameEvent.ApplicationName = Name;
var jObject = JsonConvert.DeserializeObject<JObject>(JsonConvert.SerializeObject(gameEvent));
PostData(Destination + "/game_event", jObject);
}
public void PostData(string url, JObject json)
{
var request = (HttpWebRequest)WebRequest.Create(url);
var postBytes = System.Text.Encoding.UTF8.GetBytes(json.ToString(Formatting.Indented));
request.Method = "POST";
request.ContentType = "application/json; charset=UTF-8";
request.Accept = "application/json";
request.ContentLength = postBytes.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(postBytes, 0, postBytes.Length);
}
try
{
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
}
catch (WebException e)
{
var responseString = new StreamReader(e.Response.GetResponseStream()).ReadToEnd();
}
}
}
}