本文整理汇总了C#中WebSocketServer.GenerateTransportSource方法的典型用法代码示例。如果您正苦于以下问题:C# WebSocketServer.GenerateTransportSource方法的具体用法?C# WebSocketServer.GenerateTransportSource怎么用?C# WebSocketServer.GenerateTransportSource使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WebSocketServer
的用法示例。
在下文中一共展示了WebSocketServer.GenerateTransportSource方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BasicRoundTrip
public void BasicRoundTrip()
{
var serializer = new JsonCommonSerializer();
var port = new Random().Next(6000, 60000);
var listener = new WebSocketServer("ws://localhost:" + port);
var serverTransport = listener.GenerateTransportSource("/p1");
var serverRouter = new DefaultMessageRouter(serverTransport, serializer);
serverRouter.AddService<IMyService>(new MyService());
listener.Start();
var client = new WebSocket("ws://localhost:" + port + "/p1");
var clientTransport = client.GenerateTransportSource();
var clientRouter = new DefaultMessageRouter(clientTransport, serializer);
var proxy = clientRouter.AddInterface<IMyService>();
client.Connect();
var result = proxy.Add(3, 4).Result;
Assert.Equal(7, result);
clientRouter.Dispose();
clientTransport.Dispose();
client.Close();
serverRouter.Dispose();
serverTransport.Dispose();
listener.Stop();
}
示例2: Benchmark
public void Benchmark()
{
var serializerSource = new Newtonsoft.Json.JsonSerializer();
var serializer =new JsonCommonSerializer(serializerSource); // new ProtobufCommonSerializer();//
var port = new Random().Next(6000, 60000);
var listener = new WebSocketServer("ws://localhost:" + port);
var serverTransport = listener.GenerateTransportSource("/p1");
var serverRouter = new DefaultMessageRouter(serverTransport, serializer);
serverRouter.AddService<ISumService>(new SumService());
listener.Start();
var client = new WebSocket("ws://localhost:" + port + "/p1");
//client.Compression = WebSocketSharp.CompressionMethod.Deflate;
var clientTransport = client.GenerateTransportSource();
var clientRouter = new DefaultMessageRouter(clientTransport, serializer);
var proxy = clientRouter.AddInterface<ISumService>();
client.Connect();
const int randCnt = 100;
var rand = new Random(42);
var randoms = new int[randCnt];
for (int i = 0; i < randCnt; i++) randoms[i] = rand.Next(10000000, 20000000);
var package = new SumPackage { Numbers = randoms };
var sw = new Stopwatch();
long timeFromClient = 0, timeToClient = 0;
const int cnt = 1000;
for (int j = 0; j < cnt; j++)
{
sw.Start();
//var sum = proxy.Sum(randoms).Result;
var sum = proxy.SumPackage(package).Result;
sw.Stop();
Assert.Equal(randoms.Sum(), sum);
for (int i = 0; i < randCnt; i++) randoms[i] = rand.Next(10000000, 20000000);
var times = proxy.TimeDiff(Stopwatch.GetTimestamp()).Result;
timeFromClient += times.Item1;
timeToClient += Stopwatch.GetTimestamp() - times.Item2;
}
_testOutputHelper.WriteLine("Completed {0} sum passes in {1}ms", cnt, sw.ElapsedMilliseconds);
_testOutputHelper.WriteLine("Client to server latency: {0}us", timeFromClient / cnt / 10);
_testOutputHelper.WriteLine("Server to client latency: {0}us", timeToClient / cnt / 10);
sw.Reset();
var tree = new SumServiceTree();
SumServiceTree.FillTree(tree, rand, 2);
_testOutputHelper.WriteLine("Starting large message transfer.");
sw.Start();
var result = proxy.Increment(tree).Result;
sw.Stop();
Assert.Equal(tree.Leaf + 1, result.Leaf);
_testOutputHelper.WriteLine("Completed large transfer in {0}ms", sw.Elapsed.TotalMilliseconds);
clientRouter.Dispose();
clientTransport.Dispose();
client.Close();
serverRouter.Dispose();
serverTransport.Dispose();
listener.Stop();
}