forked from vrcx-team/VRCX
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OVR Toolkit Notifications (vrcx-team#751)
* Added OVR Toolkit notification support * Change order * Self identify by user-agent. * Fix comments
- Loading branch information
1 parent
847796b
commit 3b6d289
Showing
6 changed files
with
516 additions
and
2 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net.WebSockets; | ||
using System.Threading.Tasks; | ||
using Websocket.Client; | ||
|
||
namespace VRCX | ||
{ | ||
public partial class AppApi | ||
{ | ||
private static readonly Uri _ovrtWebsocketUri = new("ws://127.0.0.1:11450/api"); | ||
private IWebsocketClient _ovrtWebsocketClient; | ||
private object _ovrtLock = new(); | ||
|
||
public void Initialize() | ||
{ | ||
lock (_ovrtLock) | ||
{ | ||
if (_ovrtWebsocketClient != null) | ||
return; | ||
|
||
var dotnetWebsocketClientFactory = new Func<ClientWebSocket>(() => | ||
{ | ||
var client = new ClientWebSocket | ||
{ | ||
Options = | ||
{ | ||
KeepAliveInterval = TimeSpan.FromSeconds(5), | ||
} | ||
}; | ||
client.Options.SetRequestHeader("user-agent", Program.Version); | ||
return client; | ||
}); | ||
|
||
_ovrtWebsocketClient = new WebsocketClient(_ovrtWebsocketUri, dotnetWebsocketClientFactory) | ||
{ | ||
Name = "OVRToolkit Websocket", | ||
// Swap ReconnectTimeout when Ping is implemented | ||
// ReconnectTimeout = TimeSpan.FromSeconds(20), | ||
ReconnectTimeout = null, | ||
ErrorReconnectTimeout = TimeSpan.FromSeconds(30), | ||
}; | ||
|
||
_ovrtWebsocketClient.ReconnectionHappened.Subscribe(info => | ||
{ | ||
logger.ConditionalDebug("[OVRToolkit Websocket] Reconnection happened, type: {0}", info?.Type.ToString()); | ||
}); | ||
_ovrtWebsocketClient.DisconnectionHappened.Subscribe(info => | ||
{ | ||
logger.ConditionalDebug("[OVRToolkit Websocket] Disconnection happened, type: {0}", info?.Type.ToString()); | ||
}); | ||
_ovrtWebsocketClient.MessageReceived.Subscribe(msg => | ||
{ | ||
logger.ConditionalDebug("[OVRToolkit Websocket] Message received: {0}", msg.Text); | ||
}); | ||
|
||
_ovrtWebsocketClient.Start().Wait(); | ||
|
||
// Uncomment when Ping is implemented | ||
// Task.Run(PingLoop); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Displays an OVRToolkit notification with the specified title and body. | ||
/// HUD ntoficcation - Visible in the lower part of the HMD view and moves with the head. | ||
/// </summary> | ||
/// <param name="hudNotification">Whether or not to display a HUD notification.</param> | ||
/// <param name="wristNotification">Whether or not to display a Wrist notification.</param> | ||
/// <param name="title">The title of the notification.</param> | ||
/// <param name="body">The content of the notification.</param> | ||
/// <param name="timeout">[CURRENTLY UNUSED]The timeout of the notification.</param> | ||
/// <param name="image">[CURRENTLY UNUSED]The image of the notification.</param> | ||
public void OVRTNotification(bool hudNotification, bool wristNotification, string title, string body, int timeout, string image = "") | ||
{ | ||
List<OVRTMessage> messages = []; | ||
|
||
// Uncomment when Image notification is implemented | ||
/* | ||
if(!string.IsNullOrWhiteSpace(image) && File.Exists(image)) | ||
{ | ||
image = Convert.ToBase64String(File.ReadAllBytes(image)); | ||
} | ||
else | ||
{ | ||
image = "iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAACXBIWXMAAAsTAAALEwEAmpwYAAAHaGlUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4gPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iQWRvYmUgWE1QIENvcmUgNS42LWMxNDUgNzkuMTYzND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Type = "SendNotification", | ||
json = System.Text.Json.JsonSerializer.Serialize(new OVRTNotificationMessage | ||
{ | ||
title = title, | ||
body = body | ||
}) | ||
}); | ||
} | ||
|
||
if (wristNotification) | ||
{ | ||
messages.Add(new OVRTMessage | ||
{ | ||
messageType = "SendWristNotification", | ||
json = System.Text.Json.JsonSerializer.Serialize(new OVRTNotificationMessage | ||
{ | ||
body = title + " - " + body | ||
}) | ||
}); | ||
} | ||
|
||
if (messages.Count > 0) | ||
{ | ||
if (_ovrtWebsocketClient == null) | ||
Initialize(); | ||
|
||
if (_ovrtWebsocketClient.IsRunning) | ||
{ | ||
foreach (var message in messages) | ||
{ | ||
_ovrtWebsocketClient.Send(System.Text.Json.JsonSerializer.Serialize(message)); | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
private async Task PingLoop() | ||
{ | ||
var pingMessage = System.Text.Json.JsonSerializer.Serialize(new OVRTMessage | ||
{ | ||
messageType = "Ping", | ||
json = string.Empty, | ||
}); | ||
|
||
while (true) | ||
{ | ||
await Task.Delay(5000); | ||
|
||
if (_ovrtWebsocketClient?.IsRunning != true) | ||
continue; | ||
|
||
_ovrtWebsocketClient?.Send(pingMessage); | ||
} | ||
} | ||
|
||
private struct OVRTMessage | ||
{ | ||
public string messageType { get; set; } | ||
public string json { get; set; } | ||
} | ||
|
||
private struct OVRTNotificationMessage | ||
{ | ||
public string title { get; set; } | ||
public string body { get; set; } | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.