gpt4 book ai didi

java - Android 应用未从 SignalR 集线器接收数据

转载 作者:搜寻专家 更新时间:2023-10-30 21:28:07 34 4
gpt4 key购买 nike

我已经阅读了这些主题:
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<string> _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("<Debug", "new message received in subscribe"); // won't work!
}
}

// this event never fired!
hub.on("NewMessage", new SubscriptionHandler() {
@Override
public void run() {
Log.d("<Debug", "new message received in `on`"); // won't work!
}
});

// connect to the server that works fine.
SignalRFuture<Void> 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<Void>() {
@Override
public void run(Void aVoid) throws Exception {
Log.d("<Debug", "message sent."); // Works fine
}
});

}
}

在上面的 java 代码中,调用服务器上的 sendMessage 工作正常,服务器收到消息。
但唯一的问题是服务器永远不会调用 hub.on(...)hub.subscribe(...) 事件。
简单的说,我的app可以发消息,但是收不到别人的消息。
任何建议将不胜感激。

最佳答案

对于 future ,这是我最终获得答案的方式(请先阅读问题android代码):

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

/* ****new codes here**** */
hub.subscribe(this);

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

/* ****new codes here**** */
connection.start(transport);

/* ****new codes here**** */
/* ****seems useless but should be here!**** */
hub.subscribe(new Object() {
@SuppressWarnings("unused")
public void newMessage(final String message, final String messageId, final String chatId,
final String senderUserId, final String fileUrl, final String replyToMessageId) {


}
});


/* ********************** */
/* ****new codes here**** */
/* **** the main method that I fetch data from server**** */
connection.received(new MessageReceivedHandler() {
@Override
public void onMessageReceived(final JsonElement json) {
runOnUiThread(new Runnable() {
public void run() {
JsonObject jsonObject = json.getAsJsonObject();
Log.e("<Debug>", "response = " + jsonObject.toString());

}
});
}
});
/* ********************** */

}
}

!重要说明:
代码的优先级很重要。这就是我解决本主题问题的方法。

关于java - Android 应用未从 SignalR 集线器接收数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39595250/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com