0.6 beta2 - Added api/gateway

This commit is contained in:
RogueException
2015-08-26 22:47:28 -03:00
parent 9d06e2b48d
commit 2086a0fa04
6 changed files with 23 additions and 17 deletions

View File

@@ -11,7 +11,9 @@ namespace Discord.API
public const int MaxMessageSize = 2000; public const int MaxMessageSize = 2000;
//Auth //Auth
public static async Task<APIResponses.AuthRegister> LoginAnonymous(string username) public static Task<APIResponses.Gateway> GetWebSocket()
=> Http.Get<APIResponses.Gateway>(Endpoints.Gateway);
public static async Task<APIResponses.AuthRegister> LoginAnonymous(string username)
{ {
var fingerprintResponse = await Http.Post<APIResponses.AuthFingerprint>(Endpoints.AuthFingerprint); var fingerprintResponse = await Http.Post<APIResponses.AuthFingerprint>(Endpoints.AuthFingerprint);
var registerRequest = new APIRequests.AuthRegisterRequest { Fingerprint = fingerprintResponse.Fingerprint, Username = username }; var registerRequest = new APIRequests.AuthRegisterRequest { Fingerprint = fingerprintResponse.Fingerprint, Username = username };

View File

@@ -9,7 +9,8 @@
// /api // /api
public static readonly string BaseApi = $"{BaseHttps}/api"; public static readonly string BaseApi = $"{BaseHttps}/api";
public static readonly string Track = $"{BaseApi}/track"; //public static readonly string Track = $"{BaseApi}/track";
public static readonly string Gateway = $"{BaseApi}/gateway";
// /api/auth // /api/auth
public static readonly string Auth = $"{BaseApi}/auth"; public static readonly string Auth = $"{BaseApi}/auth";
@@ -49,9 +50,6 @@
public static readonly string VoiceRegions = $"{Voice}/regions"; public static readonly string VoiceRegions = $"{Voice}/regions";
public static readonly string VoiceIce = $"{Voice}/ice"; public static readonly string VoiceIce = $"{Voice}/ice";
//Web Sockets
public static readonly string WebSocket_Hub = $"{BaseUrl}/hub";
//Website //Website
public static string InviteUrl(string code) => $"{BaseShortHttps}/{code}"; public static string InviteUrl(string code) => $"{BaseShortHttps}/{code}";
} }

View File

@@ -9,6 +9,12 @@ namespace Discord.API.Models
{ {
internal static class APIResponses internal static class APIResponses
{ {
public class Gateway
{
[JsonProperty(PropertyName = "url")]
public string Url;
}
public class AuthFingerprint public class AuthFingerprint
{ {
[JsonProperty(PropertyName = "fingerprint")] [JsonProperty(PropertyName = "fingerprint")]

View File

@@ -301,7 +301,7 @@ namespace Discord
try try
{ {
await Task.Delay(_config.ReconnectDelay); await Task.Delay(_config.ReconnectDelay);
await _webSocket.ConnectAsync(Endpoints.WebSocket_Hub); await _webSocket.ReconnectAsync();
await _webSocket.Login(); await _webSocket.Login();
break; break;
} }
@@ -587,7 +587,7 @@ namespace Discord
{ {
_currentVoiceEndpoint = data.Endpoint.Split(':')[0]; _currentVoiceEndpoint = data.Endpoint.Split(':')[0];
_currentVoiceToken = data.Token; _currentVoiceToken = data.Token;
await _voiceWebSocket.ConnectAsync(_currentVoiceEndpoint); await _voiceWebSocket.ConnectAsync("wss://" + _currentVoiceEndpoint);
await _voiceWebSocket.Login(_currentVoiceServerId, UserId, SessionId, _currentVoiceToken); await _voiceWebSocket.Login(_currentVoiceServerId, UserId, SessionId, _currentVoiceToken);
} }
#endif #endif
@@ -859,12 +859,14 @@ namespace Discord
_blockEvent.Reset(); _blockEvent.Reset();
_disconnectToken = new CancellationTokenSource(); _disconnectToken = new CancellationTokenSource();
string url = (await DiscordAPI.GetWebSocket()).Url;
//Connect by Token //Connect by Token
if (token != null) if (token != null)
{ {
try try
{ {
await _webSocket.ConnectAsync(Endpoints.WebSocket_Hub); await _webSocket.ConnectAsync(url);
Http.Token = token; Http.Token = token;
await _webSocket.Login(); await _webSocket.Login();
success = true; success = true;
@@ -878,7 +880,7 @@ namespace Discord
if (!success && password != null) //Email/Password login if (!success && password != null) //Email/Password login
{ {
//Open websocket while we wait for login response //Open websocket while we wait for login response
Task socketTask = _webSocket.ConnectAsync(Endpoints.WebSocket_Hub); Task socketTask = _webSocket.ConnectAsync(url);
var response = await DiscordAPI.Login(emailOrUsername, password); var response = await DiscordAPI.Login(emailOrUsername, password);
await socketTask; await socketTask;
@@ -891,7 +893,7 @@ namespace Discord
if (!success && password == null) //Anonymous login if (!success && password == null) //Anonymous login
{ {
//Open websocket while we wait for login response //Open websocket while we wait for login response
Task socketTask = _webSocket.ConnectAsync(Endpoints.WebSocket_Hub); Task socketTask = _webSocket.ConnectAsync(url);
var response = await DiscordAPI.LoginAnonymous(emailOrUsername); var response = await DiscordAPI.LoginAnonymous(emailOrUsername);
await socketTask; await socketTask;

View File

@@ -1,14 +1,10 @@
using Discord.API.Models; using Newtonsoft.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Net.WebSockets; using System.Net.WebSockets;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Net;
using System.Linq;
namespace Discord namespace Discord
{ {
@@ -47,7 +43,7 @@ namespace Discord
_webSocket = new ClientWebSocket(); _webSocket = new ClientWebSocket();
_webSocket.Options.KeepAliveInterval = TimeSpan.Zero; _webSocket.Options.KeepAliveInterval = TimeSpan.Zero;
await _webSocket.ConnectAsync(new Uri("wss://" + url), cancelToken); await _webSocket.ConnectAsync(new Uri(url), cancelToken);
_host = url; _host = url;
OnConnect(); OnConnect();
@@ -78,6 +74,8 @@ namespace Discord
_tasks = null; _tasks = null;
}); });
} }
public Task ReconnectAsync()
=> ConnectAsync(_host);
public async Task DisconnectAsync() public async Task DisconnectAsync()
{ {
if (_tasks != null) if (_tasks != null)

View File

@@ -1,5 +1,5 @@
{ {
"version": "0.6.0-beta1", "version": "0.6.0-beta2",
"description": "An unofficial .Net API wrapper for the Discord client.", "description": "An unofficial .Net API wrapper for the Discord client.",
"authors": [ "RogueException" ], "authors": [ "RogueException" ],
"tags": [ "discord", "discordapp" ], "tags": [ "discord", "discordapp" ],