gpt4 book ai didi

android - 将 android 应用程序连接到 sockjs 服务器

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:36:29 24 4
gpt4 key购买 nike

我正在开发原生安卓应用,我必须将我的应用程序连接到 Sockjs 服务器。我尝试连接 Socket.IO、Autobahn、WebSockets,但未连接。有没有适用于android的sockjs客户端?请帮忙,谢谢。

套接字接口(interface):

private void connectToSocket(String serverUrl, String accessKey) {
IO.Options opts = new IO.Options();
opts.forceNew = true;
opts.reconnection = true;
//opts.query = "accessKey="+accessKey;
String host = serverUrl;

try {
mSocket = IO.socket(host, opts);
} catch (URISyntaxException e) {
e.printStackTrace();
}

mSocket.on(Socket.EVENT_CONNECT_ERROR, onConnectError);
mSocket.on(Socket.EVENT_CONNECT_TIMEOUT, onConnectError);
mSocket.on(Socket.EVENT_CONNECT, onConnect);
mSocket.on("new message", onNewMessage);
mSocket.connect().emit("accessKey", accessKey);
if (mSocket.connected()) {
Log.d("SocketMsg: ", "Connected");
}

}

private Emitter.Listener onConnect = new Emitter.Listener() {

@Override
public void call(final Object... args) {
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d("SocketMsg: ", args[0].toString());
//mSocket.emit("accessKey", accessKey);
Toast.makeText(MainActivity.this.getApplicationContext(),
"Connect", Toast.LENGTH_LONG).show();
for (Object o : args) {
Log.i("IO " + Socket.EVENT_CONNECT_ERROR, o.toString());
}
}
});
}
};
private Emitter.Listener onConnectError = new Emitter.Listener() {
@Override
public void call(final Object... args) {
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d("SocketMsg: ", args[0].toString());
Toast.makeText(MainActivity.this.getApplicationContext(),
"Error", Toast.LENGTH_LONG).show();
for (Object o : args) {
Log.i("IO " + Socket.EVENT_CONNECT_ERROR, o.toString());
}
}
});
}
};

它给出“xhr 轮询错误”

最佳答案

现在使用以下技巧成功连接到 SockJS :)。

1. Place a web view and set its visibility to gone.
2. Use SockJS client in webview.
3. Create a javascript Interface that send data back to native code when get data from socket.
4. We have to call the function only once from native code.

JavaScript 接口(interface):

public class WebAppInterface {
Context mContext;

/**
* Instantiate the interface and set the context
*/
WebAppInterface(Context c) {
mContext = c;
}

/**
* Show a toast from the web page
*/
@JavascriptInterface
public void msg(final String toast) {
Log.d("msg: ", toast);
if (toast.contains("{"))
createMarkerList(toast);
}
}

Webview 上的 SockJS 客户端:

<script type="text/javascript">
function connectToSocket(url, accessKey){
Android.msg(String("called"));
var sock = new SockJS("http://api.example.in:8182/realtime");
sock.onopen = function() {
Android.msg("open");
sock.send(JSON.stringify({
key: accessKey
}));
};

sock.onclose = function() {
Android.msg("close");
};

sock.onmessage = function(e) {
result = e.data.replace(/,\s*$/, '');
Android.msg(String(result));
};

sock.onerror = function(e) {
Android.msg("error");
};
}
</script>

调用js函数:

webView.loadUrl(javascript:connectToSocket(url, accessKey));

关于android - 将 android 应用程序连接到 sockjs 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31645088/

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