Skip to content

Commit

Permalink
Peer type update
Browse files Browse the repository at this point in the history
  • Loading branch information
LostBeard committed Apr 4, 2024
1 parent 310cedb commit 449227e
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,10 @@
// TODO - add dialog with add torrent options (also use it when adding torrent via magnet)
WebTorrentService.Client!.Add(uint8array);
}
catch { }
catch(Exception ex)
{
JS.Log("torrent_file_picker failed:", ex.Message);
}
file.Dispose();
}
}
Expand Down
58 changes: 29 additions & 29 deletions SpawnDev.BlazorJS.WebTorrents.Demo/wwwroot/fetch-stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,33 +137,33 @@
}
}
};
var useWebSocketProxy = false;
var WebSocketOrig = WebSocket;
var blockedHostTarget = 'wss://localhost:65535/host_blocked';
var WebSocket = function (wsUrl, protocols) {
var url = new URL(wsUrl);
var hostname = url.hostname;
var isBlocked = fetchStats.isHostBlocked(hostname);
//console.log('new WebSocket', hostname, wsUrl, protocols);
if (isBlocked) {
//console.log('Blocked WebSocket connection to host', hostname);
wsUrl = blockedHostTarget;
}
var ws = new WebSocketOrig(wsUrl, protocols);
if (!useWebSocketProxy) return ws;
var overrides = {
//
};
var wsp = new Proxy(ws, {
get(target, key) {
if (key in overrides) {
return overrides[key];
}
var ret = Reflect.get(target, key, this);
return ret;
}
});
return wsp;
}
globalThis.WebSocket = WebSocket;
//var useWebSocketProxy = false;
//var WebSocketOrig = WebSocket;
//var blockedHostTarget = 'wss://localhost:65535/host_blocked';
//var WebSocket = function (wsUrl, protocols) {
// var url = new URL(wsUrl);
// var hostname = url.hostname;
// var isBlocked = fetchStats.isHostBlocked(hostname);
// //console.log('new WebSocket', hostname, wsUrl, protocols);
// if (isBlocked) {
// //console.log('Blocked WebSocket connection to host', hostname);
// wsUrl = blockedHostTarget;
// }
// var ws = new WebSocketOrig(wsUrl, protocols);
// if (!useWebSocketProxy) return ws;
// var overrides = {
// //
// };
// var wsp = new Proxy(ws, {
// get(target, key) {
// if (key in overrides) {
// return overrides[key];
// }
// var ret = Reflect.get(target, key, this);
// return ret;
// }
// });
// return wsp;
//}
//globalThis.WebSocket = WebSocket;
})();
20 changes: 20 additions & 0 deletions SpawnDev.BlazorJS.WebTorrents/DiscoveredPeer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.JSInterop;

namespace SpawnDev.BlazorJS.WebTorrents
{
public class DiscoveredPeer : JSObject
{
/// <summary>
/// Deserialization constructor
/// </summary>
/// <param name="_ref"></param>
public DiscoveredPeer(IJSInProcessObjectReference _ref) : base(_ref) { }
public string Id => JSRef.Get<string>("id");
public string ChannelName => JSRef.Get<string>("channelName");
public bool Initiator => JSRef.Get<bool>("initiator");
public bool Trickle => JSRef.Get<bool>("trickle");
public string? RemoteAddress => JSRef.Get<string?>("remoteAddress");
public string? RemoteFamily => JSRef.Get<string?>("remoteFamily");
public ushort? RemotePort => JSRef.Get<ushort?>("remotePort");
}
}
2 changes: 1 addition & 1 deletion SpawnDev.BlazorJS.WebTorrents/Discovery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ public Discovery(IJSInProcessObjectReference _ref) : base(_ref) { }
/// peer Peer - the peer<br />
/// source string - the peer source
/// </summary>
public JSEventCallback<Peer, string> OnPeer { get => new JSEventCallback<Peer, string>("peer", On, RemoveListener); set { } }
public JSEventCallback<DiscoveredPeer, string> OnPeer { get => new JSEventCallback<DiscoveredPeer, string>("peer", On, RemoveListener); set { } }
}
}
17 changes: 8 additions & 9 deletions SpawnDev.BlazorJS.WebTorrents/Peer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@

namespace SpawnDev.BlazorJS.WebTorrents
{
/// <summary>
/// Peer class
/// </summary>
public class Peer : JSObject
{
/// <summary>
/// Deserialization constructor
/// </summary>
/// <param name="_ref"></param>
public Peer(IJSInProcessObjectReference _ref) : base(_ref) { }
public string ChannelName => JSRef.Get<string>("channelName");
public string Id => JSRef.Get<string>("id");
public bool Initiator => JSRef.Get<bool>("initiator");
public bool Trickle => JSRef.Get<bool>("trickle");
public string? RemoteAddress => JSRef.Get<string?>("remoteAddress");
public string? RemoteFamily => JSRef.Get<string?>("remoteFamily");
public ushort? RemotePort => JSRef.Get<ushort?>("remotePort");
public string Type => JSRef.Get<string>("type");
public bool Destroyed => JSRef.Get<bool>("destroyed");
public bool Connected => JSRef.Get<bool>("connected");
public bool SendHandshake => JSRef.Get<bool>("sendHandshake");
public int HandshakeTimeout => JSRef.Get<int>("handshakeTimeout");
public int Retries => JSRef.Get<int>("retries");
public Wire? Wire => JSRef.Get<Wire>("wire");
public Torrent? Swarm => JSRef.Get<Torrent?>("swarm");
}
}
4 changes: 4 additions & 0 deletions SpawnDev.BlazorJS.WebTorrents/Torrent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public Torrent(IJSInProcessObjectReference _ref) : base(_ref) { }
/// </summary>
public Array<Wire> Wires => JSRef.Get<Array<Wire>>("wires");
/// <summary>
/// Peers (peers underpin wires)
/// </summary>
public Dictionary<string, Peer> Peers => JSRef.Get<Dictionary<string, Peer>>("_peers");
/// <summary>
/// Torrent storage
/// </summary>
public TorrentStore Store => JSRef.Get<TorrentStore>("store");
Expand Down
2 changes: 1 addition & 1 deletion SpawnDev.BlazorJS.WebTorrents/Tracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class Tracker : EventEmitter
public Tracker(IJSInProcessObjectReference _ref) : base(_ref) { }
public JSEventCallback<JSObject> OnWarning { get => new JSEventCallback<JSObject>("warning", On, RemoveListener); set { } }
public JSEventCallback<JSObject> OnError { get => new JSEventCallback<JSObject>("error", On, RemoveListener); set { } }
public JSEventCallback<Peer> OnPeer { get => new JSEventCallback<Peer>("peer", On, RemoveListener); set { } }
public JSEventCallback<DiscoveredPeer> OnPeer { get => new JSEventCallback<DiscoveredPeer>("peer", On, RemoveListener); set { } }
public JSEventCallback<TrackerUpdateMessage> OnUpdate { get => new JSEventCallback<TrackerUpdateMessage>("update", On, RemoveListener); set { } }
public string UserAgent => JSRef.Get<string>("_userAgent");
public Array<TrackerConnection> Trackers => JSRef.Get<Array<TrackerConnection>>("_trackers");
Expand Down
2 changes: 1 addition & 1 deletion SpawnDev.BlazorJS.WebTorrents/WebTorrentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ void WebTorrent_OnAdd(Torrent torrent)
torrent.OnError += onError;
OnTorrentAdd?.Invoke(torrent);
}
void Discovery_OnPeer(Peer peer, string source)
void Discovery_OnPeer(DiscoveredPeer peer, string source)
{
if (Verbose) JS.Log("Discovery_OnPeer", peer, source);
}
Expand Down

0 comments on commit 449227e

Please sign in to comment.