fixing single client invocation

This commit is contained in:
moozzyk 2016-10-11 16:17:23 -07:00
parent 5d41b218f0
commit e1869d29a4
1 changed files with 16 additions and 18 deletions

View File

@ -1,12 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Channels;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using SocketsSample.Hubs;
namespace SocketsSample
@ -31,20 +28,6 @@ namespace SocketsSample
return new SingleClientProxy(this, connectionId);
}
private byte[] Pack(string method, object[] args)
{
var obj = new JObject();
obj["method"] = method;
obj["params"] = new JArray(args.Select(a => JToken.FromObject(a)).ToArray());
if (_logger.IsEnabled(LogLevel.Debug))
{
_logger.LogDebug("Outgoing RPC invocation method '{methodName}'", method);
}
return Encoding.UTF8.GetBytes(obj.ToString());
}
protected override void Initialize(object endpoint)
{
((Hub)endpoint).Clients = this;
@ -103,7 +86,22 @@ namespace SocketsSample
public Task Invoke(string method, params object[] args)
{
var connection = _endPoint.Connections[_connectionId];
return connection?.Channel.Output.WriteAsync(_endPoint.Pack(method, args));
var invocationAdapter = _endPoint._serviceProvider.GetRequiredService<SocketFormatters>()
.GetInvocationAdapter((string)connection.Metadata["formatType"]);
if (_endPoint._logger.IsEnabled(LogLevel.Debug))
{
_endPoint._logger.LogDebug("Outgoing RPC invocation method '{methodName}'", method);
}
var message = new InvocationDescriptor
{
Method = method,
Arguments = args
};
return invocationAdapter.InvokeClientMethod(connection.Channel.GetStream(), message);
}
}
}