- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我已经阅读了这些主题:
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/
我想知道如何存储在调用时可以触发到连接的客户端的变量服务器端? 我使用并发字典来跟踪连接 GUID 并想知道我是否可以在服务器上存储字符串或其他数据类型,例如最后 10 条消息,因此当出现新连接时,客
无法让 SignalR 在我的机器上工作(使用 IE9)。在输入一些文本并单击提交时,文本未按预期列出。另外,我希望从多个浏览器实例更新列表,但它不会发生。没有错误。有人可以帮忙吗? C# names
找到后this update围绕与组的通信,似乎一个组与集线器名称无关。如果这是正确的(如果我弄错了,请告诉我)集线器有什么方法可以访问另一个集线器的组,或者更好地拥有某种全局组? 我的问题是我有一个
抽象的 嗨,我在考虑是否可以使用 SignalR 发送消息。假设客户端断开连接,但最终在很短的时间内(例如 3 秒)重新连接。客户端会在断开连接时收到所有发送给他的消息吗? 例如,让我们考虑 Long
我正在为中等负载的 Web 应用程序评估 SignalR。 我们期望大约 500 条消息/秒,这对 SignalR 来说应该不是问题。 但是,我们担心此解决方案的可靠性。我们的环境有一个有问题的网络,
我有以下JS工作: var chat = $.connection.appHub; 我的应用程序有一个单一的中心AppHub,它处理两种类型的通知-Chat和Other。我使用一个集线器,因为我需要一
我升级到.NET 4.5,现在SignalR似乎坚持要在Firefox / Chrome中使用WebSockets-即使我只是在没有WebSocket Server的Windows 7上使用。 该请求
您能否在服务器端辨别给定的SignalR连接使用哪种传输方法? (WebSocket,SSE,长时间轮询等?) 最佳答案 在集线器内部,您可以通过查看请求的查询字符串来检测正在使用的传输: Conte
我在MVC4应用程序中使用Visual Studio 2012 Ultimate RC,SignalR 0.5.1和Jquery 1.7.2。 我看过:MVC4 SignalR "signalr/hu
假设我的网络连接中断了几秒钟,并且我错过了一些 SignalR 服务器推送的消息。 当我重新获得网络连接时,我错过的消息是否会丢失?或者当我重新连接时 signalR 会处理它们并将它们推出吗? 如果
如果我有一个 signalR 集线器,它向所有具有相当大的有效负载或私有(private)信息的客户端发送消息,那么所有连接到该集线器的客户端(同一组的一部分)是否都会收到消息,即使它们没有订阅客户端
如果我有一个 signalR 集线器,它向所有具有相当大的有效负载或私有(private)信息的客户端发送消息,那么所有连接到该集线器的客户端(同一组的一部分)是否都会收到消息,即使它们没有订阅客户端
谁能让我知道 SignalR 内部是如何以高级方式工作的? 我猜测它正在使用 Response.Flush 刷新数据,并且在客户端以一定的时间间隔发送 Ajax 请求。正确吗? 最佳答案 不,Sign
我正在使用 Microsoft SignalR 向浏览器推送通知。这些通知是由其他浏览器的操作触发的。我想做一个有时发送通知的后台任务。例如,在 12:45:21,我想向所有连接的用户发出通知,即使他
我的 SignalR 存在以下问题,用户从移动设备访问我的网站。 问题在于,当手机锁定或用户转到另一个应用程序时,会在服务器上触发 Disconnect() 方法(我尝试向服务器发送消息并等待它通过
如果我使用 SignalR 发送消息,客户端是否可能收不到消息?您如何验证通信中是否出现任何错误?我想在发送服务器通知后将消息发送回服务器,但有没有更好的方法? 最佳答案 是的,客户端可能没有收到消息
Web 服务器如何使用 SignalR 同步调用客户端上的方法?这里的关键部分是服务器应该等待客户端将数据发送回服务器才能继续? 基本上,我希望将客户端方法调用视为 WCF 服务请求。调用客户端并等待
我正在尝试调试 SignalR 连接问题,虽然我知道如何启用客户端日志记录,但我不知道如何启用服务器端日志记录。它似乎没有将任何内容记录到控制台。 我试过在 HubConfiguration 中使用
如果 SignalR 服务器的内部缓冲区溢出,则它似乎可以在将消息发送到客户端之前丢弃消息(请参阅 https://github.com/SignalR/SignalR/issues/2075 )。目
我在 Blazor 服务器端应用程序中使用 SignalR。我将 Microsoft.AspNetCore.SignalR.Client Nuget 包 (v5.0.11) 添加到项目中,并使用以下代
我是一名优秀的程序员,十分优秀!