我正在开发一个Android应用程序,我需要连接到Web套接字并接收消息以从Web服务器解锁应用程序。接收消息已通过Stompclient在java脚本上实现,但我需要在Android上执行相同的操作。我读到了关于Okhttp3及其对web socket的支持,所以我能够实现它websocket正在打开,但我不知道如何订阅在Android上接收消息。
public class LockScreenActivity extends AppCompatActivity implements ServerConnection.ServerListener{
private ServerConnection mServerConnection;
private JSONObject jsonObject;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lock_screen);
mServerConnection = new ServerConnection(TerminalDetails.WEB_SOCKET_URL);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
Timer timer = new Timer();
timer.schedule(timerTask, 10000);
}
@Override
protected void onResume() {
super.onResume();
mServerConnection.connect(this);
mServerConnection.sendMessage("Hello, Testing Connection!");
Utility.showToast(this, "Connected to the Web Socket");
}
@Override
public void onNewMessage(String message) {
try {
Utility.showToast(this, "A new message received!");
jsonObject = new JSONObject(message);
} catch (JSONException e) {
Utility.stringify(e);
}
validateResponse(jsonObject);
}
我的Android实施:
Dim dict As Object
Set dict = CreatObject("Scripting.Dictionary")
If .Cells(.Rows.Count, "L").End(xlUp).Row > 5 Then If .Cells(.Rows.Count, "L").End(xlUp).Row > 5 Then last = .Cells(.Rows.Count, "L").End(xlUp).Row Else last = 5 Else last = 5
Set rData = .Range("L5:L" & last & ",AX5:AX" & last)
For i = rData.Row To rData.Row + rData.Rows.Count - 1
If Not .Cells(i, "L").Value = "" Then If Not dict.Exists(.Cells(i, "L").Value) Then dict(.Cells(i, "L").Value) = .Cells(i, "AX").Value
Next i
.Parent.Close False
End With
需要从服务器接收解锁消息的LockScreenActivity:
parentNavigate(destination){
this.props.navigation.navigate(destination);
}
render(){
if(this.state.isLoading){
...
}
return (
<View>
<Strip props={this.state.dataSource.strips} parentNavigate={(destination) => this.parentNavigate(destination)}/>
</View>
)
}
如果需要,我会回答有关代码的更多问题。感谢