Tôi đã đọc những thông tin:
how to use SignalR in Android
Android Client doesn't get data but .net client getting data from SignalR serverứng dụng Android không nhận được dữ liệu từ SignalR hub
tôi viết một hệ thống trò chuyện đơn giản với Android hoạt động với SignalR.
Giả sử khách hàng gửi tin nhắn (bằng cách gọi SendMessage
phương thức trên máy chủ) và máy chủ sẽ gọi phương thức NewMessage
trên máy khách.
Đây là lớp của tôi ChatHub
(được đơn giản hóa) được viết bằng C#.
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);
}
}
}
Khi client android kết nối đến máy chủ, Trên phương pháp OnConnected
, id kết nối sẽ được lưu trữ trong _connectedClients
và nó hoạt động tốt.
Trong phương pháp SendMessage
của lớp ChatHub
, Chúng tôi có android id kết nối thiết bị, và tôi chắc chắn rằng thiết bị android nằm trong danh sách
Và đây là mã Andoird tôi:
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
}
});
}
}
Trong mã số java ở trên, hãy gọi sendMessage
trên máy chủ hoạt động tốt và máy chủ nhận được thư.
Nhưng vấn đề duy nhất là các sự kiện hub.on(...)
hoặc hub.subscribe(...)
không bao giờ được gọi bởi máy chủ.
Trong một mô tả đơn giản, Ứng dụng của tôi có thể gửi tin nhắn, nhưng không thể nhận tin nhắn từ những người khác.
Mọi đề xuất sẽ được đánh giá cao.
Xem http://stackoverflow.com/a/42527962/1770868 –