android signalr jar,java – Android应用程序没有从SignalR中心接收数据

我已经阅读了这些主题:

how to use SignalR in Android

Android Client doesn’t get data but .net client getting data from SignalR server

我用Android编写了一个与SignalR配合使用的简单聊天系统.

它应该是客户端发送消息(通过在服务器上调用SendMessage方法),服务器应该在客户端上调用NewMessage方法.

这是我用C#编写的ChatHub类(简化).

public class ChatHub : Hub

{

// Store the clients connections Id

static readonly List _connectedClients;

public override Task OnConnected()

{

// Keep connections id

// This section works fine and when the android device connects to the server,

// Its connection id will stored.

_connectedClients.Add(Context.ConnectionId)

//... other codes

}

public void SendMessage(string message)

{

foreach (var connectionId in _connectedClients)

{

// according to the logs

// android device connection id exists here

// and it works fine.

Clients.Client(connectionId).NewMessage(message);

}

}

}

当android客户端连接到服务器时,在OnConnected方法上,连接ID将存储在_connectedClients中,并且它可以正常工作.

在ChatHub类的SendMessage方法中,我们有android设备连接ID,我确定android设备在列表中

这是我的Andoird代码:

public class ChatActivity extends AppCompatActivity

{

// private fields

HubConnection connection;

HubProxy hub;

ClientTransport transport;

protected void onCreate(Bundle savedInstanceState) {

Logger logger = new Logger() {

@Override

public void log(String message, LogLevel logLevel) {

Log.e("SignalR", message);

}

};

Platform.loadPlatformComponent(new AndroidPlatformComponent());

connection = new HubConnection("192.168.1.100");

hub = connection.createHubProxy("chatHub"); // case insensitivity

transport = new LongPollingTransport(connection.getLogger());

// no difference when using this:

//transport = new ServerSentEventsTransport(connection.getLogger());

// this event never fired!

hub.subscribe(new Object() {

public void NewMessage(String message)

{

Log.d("

}

}

// this event never fired!

hub.on("NewMessage", new SubscriptionHandler() {

@Override

public void run() {

Log.d("

}

});

// connect to the server that works fine.

SignalRFuture awaitConnection = connection.start(transport);

try {

awaitConnection.get(); // seems useless when using this or not!

}

catch (Exception ex) {

}

// this method works fine.

hub.invoke("sendMessage", "this is a test message to the server")

.done(new Action() {

@Override

public void run(Void aVoid) throws Exception {

Log.d("

}

});

}

}

在上面的java代码中,在服务器上调用sendMessage工作正常,服务器获取消息.

但唯一的问题是服务器永远不会调用hub.on(…)或hub.subscribe(…)事件.

在一个简单的描述中,我的应用程序可以发送消息,但无法接收来自其他人的消息.

任何建议将不胜感激.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值