我正在尝试从.NET应用程序与.NET webservice连接。我正在使用github.com/SignalR/java-client。我几乎没有使用Android的经验,我无法找到解决问题的方法。我尝试使用这些示例:github.com/SignalR/java-samples来实现android客户端。在MainActivity.java中我有方法
public void buttonClick(View v)
{
HubConnection conn = new HubConnection("http://192.168.132.28:9000");
// Create the hub proxy
HubProxy proxy = conn.createHubProxy("HubClient");
proxy.subscribe(new Object() {
@SuppressWarnings("unused")
public void messageReceived(String name, String message) {
System.out.println(name + ": " + message);
}
});
// Subscribe to the error event
conn.error(new ErrorCallback() {
@Override
public void onError(Throwable error) {
error.printStackTrace();
}
});
// Subscribe to the connected event
conn.connected(new Runnable() {
@Override
public void run() {
System.out.println("CONNECTED");
}
});
// Subscribe to the closed event
conn.closed(new Runnable() {
@Override
public void run() {
System.out.println("DISCONNECTED");
}
});
// Start the connection
conn.start()
.done(new Action<Void>() {
@Override
public void run(Void obj) throws Exception {
System.out.println("Done Connecting!");
}
});
// Subscribe to the received event
conn.received(new MessageReceivedHandler() {
@Override
public void onMessageReceived(JsonElement json) {
System.out.println("RAW received message: " + json.toString());
}
});
// Read lines and send them as messages.
Scanner inputReader = new Scanner(System.in);
String line = inputReader.nextLine();
while (!"exit".equals(line)) {
proxy.invoke("send", "Console", line).done(new Action<Void>() {
@Override
public void run(Void obj) throws Exception {
System.out.println("SENT!");
}
});
line = inputReader.next();
}
inputReader.close();
conn.stop();
}
按下按钮时调用此方法。但是当我按下按钮时,我得到一个异常microsoft.aspnet.signalr.client.transport.NegotiationException: There was a problem in the negotiation with the server
我使用this webservice进行测试。 Here是来自logcat的完整日志(按下按钮后)。我禁用防火墙进行测试。我尝试连接到本地计算机上的服务器,后来又连接到远程服务器上。在这两种情况下,我都有相同的例外。我很感激任何建议。
这是webservice中的Startup类:
using Microsoft.AspNet.SignalR;
using Microsoft.Owin;
using Owin;
using WebApplication.Features.Authorization;
using WebApplication.Features.SamplePersistentConnection;
[assembly: OwinStartupAttribute(typeof(WebApplication.Startup))]
namespace WebApplication
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
app.MapSignalR();
app.MapSignalR<DemoPersistentConnection>("/Connections/DemoPersistentConnection");
app.MapSignalR<AuthorizationPersistentConnection>("/Connections/AuthorizationPersistentConnection");
app.Map("/EnableDetailedErrors", map =>
{
var hubConfiguration = new HubConfiguration
{
EnableDetailedErrors = true
};
map.MapSignalR(hubConfiguration);
});
}
}
}
这是DemoHub类:
using System;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR;
namespace WebApplication.Features.SampleHub
{
public class DemoHub : Hub
{
public override Task OnConnected()
{
return Clients.All.hubMessage("OnConnected " + Context.ConnectionId);
}
//public override Task OnDisconnected()
//{
// return Clients.All.hubMessage("OnDisconnected " + Context.ConnectionId);
//}
public override Task OnReconnected()
{
return Clients.Caller.hubMessage("OnReconnected");
}
public void SendToMe(string value)
{
Clients.Caller.hubMessage(value);
}
public void SendToConnectionId(string connectionId, string value)
{
Clients.Client(connectionId).hubMessage(value);
}
public void SendToAll(string value)
{
Clients.All.hubMessage(value);
}
public void SendToGroup(string groupName, string value)
{
Clients.Group(groupName).hubMessage(value);
}
public void JoinGroup(string groupName, string connectionId)
{
if (string.IsNullOrEmpty(connectionId))
{
connectionId = Context.ConnectionId;
}
Groups.Add(connectionId, groupName);
Clients.All.hubMessage(connectionId + " joined group " + groupName);
}
public void LeaveGroup(string groupName, string connectionId)
{
if (string.IsNullOrEmpty(connectionId))
{
connectionId = Context.ConnectionId;
}
Groups.Remove(connectionId, groupName);
Clients.All.hubMessage(connectionId + " left group " + groupName);
}
public void IncrementClientVariable()
{
Clients.Caller.counter = Clients.Caller.counter + 1;
Clients.Caller.hubMessage("Incremented counter to " + Clients.Caller.counter);
}
public void ThrowOnVoidMethod()
{
throw new InvalidOperationException("ThrowOnVoidMethod");
}
public async Task ThrowOnTaskMethod()
{
await Task.Delay(TimeSpan.FromSeconds(1));
throw new InvalidOperationException("ThrowOnTaskMethod");
}
public void ThrowHubException()
{
throw new HubException("ThrowHubException", new { Detail = "I can provide additional error information here!" });
}
public void StartBackgroundThread()
{
BackgroundThread.Enabled = true;
BackgroundThread.SendOnPersistentConnection();
BackgroundThread.SendOnHub();
}
public void StopBackgroundThread()
{
BackgroundThread.Enabled = false;
}
}
P.S。我为在github的链接中不使用https而道歉,但新用户只允许创建2个链接。