gpt4 book ai didi

java - jquery signalr 客户端工作-但在 java 中它不能工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:40:40 28 4
gpt4 key购买 nike

我们有一个 SignalR 集线器。以下 jQuery 代码在 cordova 上成功连接并正确处理 SignalR 客户端“调用”事件。

        var connectionURL = "https://SOMEURL.azurewebsites.net/message";
connection = $.connection(connectionURL);

connection.start().done(function () {
console.log("Connected to hub again");
});

connection.disconnected(function () {
setTimeout(function () {
connection.start().done(function () {
console.log("Disconnected and Connected to hub again");
});
}, 5000);
});

connection.stateChanged(function (change) {
if (change.newState == $.signalR.connectionState.reconnecting) {
}
else if (change.newState == $.signalR.connectionState.connected) {
}
else if (change.newState == $.signalR.connectionState.disconnected) {
}// else if
});

connection.received(function (data) {
connectId = connection.id + "";
console.log("onDeviceReady run");

// call the function to parse the data
if (data.PayloadType == "Dispatch") {
dataDispatch(data);
}
if (data.PayloadType == "ConnectionAcknowledge") {
sendConnectionAcknowledge(data);
}
});

但是,当我尝试使用 SignalR Java Client 在 java android 中模拟此代码时,我得到了很多日志输出(吨),并且没有连接完成。它到达 awaitConnection.get(); 行的调试并且从不打印调试的第二行,而是打印无穷无尽(数千)行的半乱码(它不是管道,就像这是某种“SSL 握手”,但它除了重复记录相同的事情之外什么也没做,非常奇怪)无论如何它从不运行“我的”调试的第二行

package com.some.thing;

import android.util.Log;

import java.util.concurrent.ExecutionException;

import microsoft.aspnet.signalr.client.Platform;
import microsoft.aspnet.signalr.client.SignalRFuture;
import microsoft.aspnet.signalr.client.http.android.AndroidPlatformComponent;
import microsoft.aspnet.signalr.client.hubs.HubConnection;
import microsoft.aspnet.signalr.client.hubs.HubProxy;
import microsoft.aspnet.signalr.client.hubs.SubscriptionHandler1;

public class SignalRClient {

public static void startConnection() {

Platform.loadPlatformComponent(new AndroidPlatformComponent());
String host = "https://SOMEURL.azurewebsites.net/message";
HubConnection connection = new HubConnection(host);
HubProxy hub = connection.createHubProxy( "IDoNoHaveThisNorKnowIt" );

SignalRFuture<Void> awaitConnection = connection.start();
try {
Log.v("CONANSignalR :=", "CONNECTING");
awaitConnection.get();
Log.v("CONANSignalR :=", "CONNECTED");
} catch (InterruptedException e) {
// Handle ...
} catch (ExecutionException e) {
// Handle ...
}

hub.on("IDoNotKnowThisEither", new SubscriptionHandler1<String>(){
@Override
public void run( String status ){
Log.v("CONANDispatch :=", status);
}
}, String.class);
}
}

谁能帮我将有效的 jQuery SignalR 客户端代码翻译成可用的 Java 客户端代码?我没有关于集线器的任何信息,所以我不知道代理或函数名称,我想查看所有内容(例如 jQuery)。

编辑

为了测试,我修改了原来的 jquery 代码

console.log("onDeviceReady run");

现在它说

console.log("SignalR Raw Data:" + JSON.stringify(data));

当我这样做时,这就是 jquery 返回的内容

SignalR Raw Data:{"ConnectionId":"9c4b4ba5-cb6e-4dcb-8df9-069cbf749873","OrderId":null,"SenderId":null,"PayloadType":"ConnectionAck","Message":"Welcome, you are connected to the Hub!","Payload":null,"Initiator":"HUB","Version":null}

但是这些都没有出现在 java 等价物中

connection.received(new MessageReceivedHandler() {
@Override
public void onMessageReceived(JsonElement json) {
System.out.println("RAW received message: " + json.toString());

// ADD HANDLING OF RECEIVED IN HERE
}
});

即文本“RAW received message:”根本没有出现

最佳答案

由于您不知道集线器名称,因此您必须直接在连接对象上使用处理事件(就像在 js 客户端中所做的那样)。

这是可以帮助您入门的示例代码(请注意,这没有经过测试,只是从我的脑海中写出来的):

public static void startConnection() {

Platform.loadPlatformComponent(new AndroidPlatformComponent());
String host = "https://SOMEURL.azurewebsites.net/message";
HubConnection connection = new HubConnection(host);

// subscribe to received - equal to `connection.received(function (data)` from javascript
connection.received(new MessageReceivedHandler() {

@Override
public void onMessageReceived(JsonElement json) {
System.out.println("RAW received message: " + json.toString());

// ADD HANDLING OF RECEIVED IN HERE
}
});

// equal to `connection.disconnected(function ()` from javascript
connection.closed(new Runnable() {

@Override
public void run() {
// ADD CODE TO HANDLE DISCONNECTED EVENT
}
});

// equal to `connection.stateChanged(function (change)`
connection.stateChanged(new StateChangedCallback() {

@Override
public void stateChanged(ConnectionState oldState, ConnectionState newState) {
// ADD CODE TO HANDLE STATE CHANGES
}
});

// start the connection
connection.start()
.done(new Action<Void>() {

@Override
public void run(Void obj) throws Exception {
System.out.println("Connected");
}
});
}

此外,我建议您查看 java 聊天客户端示例,可在此处找到:https://github.com/SignalR/java-samples/blob/master/signalr-sample-chat/src/microsoft/aspnet/signalr/samples/chat/Program.java

我发布的大部分代码都是基于那个。

关于java - jquery signalr 客户端工作-但在 java 中它不能工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29497571/

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