-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6bec589
commit 417082a
Showing
5 changed files
with
558 additions
and
425 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 |
---|---|---|
@@ -1,56 +1,75 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using WebSocket4Net.AspNetCore.SignalR.Client; | ||
using WebSocket4Net.AspNetCore.SignalRClient.Connection; | ||
|
||
namespace ClientApp { | ||
public class Program { | ||
static void Main(string[] args) { | ||
var header = new Dictionary<string, string>(); | ||
header.Add("Sample", "Token"); | ||
namespace ClientApp | ||
{ | ||
public class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
var header = new Dictionary<string, string>(); | ||
header.Add("Sample", "Token"); | ||
|
||
var connection = new HubConnectionBuilder() | ||
.WithUrl("http://127.0.0.1:5000/chatHub", option => { | ||
option.Headers = header; | ||
var connection = new HubConnectionBuilder() | ||
.WithUrl("http://127.0.0.1:5000/chatHub", option => | ||
{ | ||
option.Headers = header; | ||
|
||
}) | ||
.ConfigService(service => { | ||
service.AddLogging(config => { | ||
config.AddConsole(); | ||
}) | ||
.ConfigService(service => | ||
{ | ||
service.AddLogging(config => | ||
{ | ||
config.AddConsole(); | ||
}); | ||
}) | ||
.Build(); | ||
connection.StartAsync().GetAwaiter().GetResult(); | ||
|
||
connection.On<UserAndMessage>("ReceiveMessage", model => | ||
{ | ||
Console.WriteLine($"user:{model.User},mes:{model.Message}"); | ||
}); | ||
}) | ||
.Build(); | ||
connection.StartAsync().GetAwaiter().GetResult(); | ||
|
||
connection.On<UserAndMessage>("ReceiveMessage", model => { | ||
Console.WriteLine($"user:{model.User},mes:{model.Message}"); | ||
}); | ||
connection.Closed += async (ex) => | ||
{ | ||
//重试几次 | ||
|
||
connection.Closed += async (ex) => { | ||
Console.WriteLine(ex.Message); | ||
//重试几次 | ||
await connection.RestartAsync(); | ||
}; | ||
connection.Send("SendMessage", new object[] { "user1", "message1" }).GetAwaiter().GetResult(); | ||
Timer timer = new Timer(obj => { | ||
connection.Invoke<UserAndMessage>("SendMessage", new object[] { "user1", "message1" }, (result, exception) => { | ||
Console.WriteLine($"result:{result}"); | ||
try | ||
{ | ||
Console.WriteLine("开始尝试连接"); | ||
await connection.RestartAsync(); | ||
} | ||
catch(Exception e) | ||
{ | ||
|
||
} | ||
}; | ||
connection.Send("SendMessage", new object[] { "user1", "message1" }).GetAwaiter().GetResult(); | ||
Timer timer = new Timer(obj => | ||
{ | ||
connection.Invoke<UserAndMessage>("SendMessage", new object[] { "user1", "message1" }, (result, exception) => | ||
{ | ||
Console.WriteLine($"result:{result}"); | ||
|
||
|
||
}).GetAwaiter().GetResult(); | ||
}, "", 0, 5 * 60 * 1000); | ||
|
||
Console.ReadKey(); | ||
} | ||
}).GetAwaiter().GetResult(); | ||
}, "", 0, 5 * 60 * 1000); | ||
|
||
Console.ReadKey(); | ||
} | ||
|
||
public class UserAndMessage { | ||
public string User { get; set; } | ||
public string Message { get; set; } | ||
public class UserAndMessage | ||
{ | ||
public string User { get; set; } | ||
public string Message { 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
Oops, something went wrong.