我正在尝试连接到具有房间ID的频道,并希望通过onReceive操作获取一些数据。我做到了,这是android系统,效果很好,但在iOS上,它总是给我失败的响应。我试图将我的android代码转换为iOS,但不确定是否丢失任何内容。以下是我的android方法。
private void subscribe() throws URISyntaxException {
URI uri = new URI("wss://mycompany.com/cable");
Consumer.Options options=new Consumer.Options();
options.headers = new ArrayMap<>();
options.headers.put("Origin","https://mycompany.com");
Consumer consumer = ActionCable.createConsumer(uri,options);
// 2. Create subscription
Channel appearanceChannel = new Channel("RoomChannel");
appearanceChannel.addParam("room_id",randomString);
Subscription subscription = consumer.getSubscriptions().create(appearanceChannel);
subscription
.onConnected(new Subscription.ConnectedCallback() {
@Override
public void call() {
// Called when the subscription has been successfully completed
}
}).onRejected(new Subscription.RejectedCallback() {
@Override
public void call() {
// Called when the subscription is rejected by the server
}
}).onReceived(new Subscription.ReceivedCallback() {
@Override
public void call(JsonElement data) {
//I get the data response here
return;
}
}).onDisconnected(new Subscription.DisconnectedCallback() {
@Override
public void call() {
// Called when the subscription has been closed
}
}).onFailed(new Subscription.FailedCallback() {
@Override
public void call(ActionCableException e) {
// Called when the subscription encounters any error
}
});
// 3. Establish connection
try {
consumer.connect();
// 4. Perform any action
subscription.perform("away");
// 5. Perform any action using JsonObject(GSON)
JsonObject params = new JsonObject();
params.addProperty("foo", "bar");
subscription.perform("appear", params);
}catch (Exception e)
{
Log.e("------error",e);
}
}
这是我的iOS方法
let url = "wss://mycompany.com/cable"
let ChannelIdentifier = "RoomChannel"
func setupActionCable(){
guard let urlString = URL(string: url) else {return}
let headers = ["Origin":"https://mycompany.com"]
self.client = ActionCableClient(url: urlString, headers:headers)
self.client?.willConnect = {
print("Will Connect")
}
self.client?.onConnected = {
print("Connected to \(String(describing: self.client?.url))")
}
self.client?.onDisconnected = {(error: ConnectionError?) in
print("Disconected with error: \(String(describing: error))")
}
self.client?.willReconnect = {
print("Reconnecting to \(String(describing: self.client?.url))")
return true
}
}
func subscribe(_ roomId:String){
let room_identifier = ["room_id" : roomId]
self.channel = client?.create(ChannelIdentifier, identifier: room_identifier)
self.channel?.onSubscribed = {
print("Subscribed to \(self.ChannelIdentifier) with room Id = \(roomId)")
}
self.channel?.onReceive = {(data: Any?, error: Error?) in
print("****** channel response data = \(String(describing: data))")
if let error = error {
print(error.localizedDescription)
return
}
}
}
我在setupActionCable
上呼叫viewDidLoad
,此后,当我获得roomId时,使用roomId呼叫subscribe
。但是在这里,我总是收到
message = {
state = FAILED;
};
但是在Android中,我在消息中收到json响应。转换为iOS时,我不确定这里缺少什么。任何帮助表示赞赏。