我已经设置了一个node.js Express服务器来连接到我的iOS套接字IO WebSockets,客户端有时仅接收事件。
插座连接良好,没有脱落,并连接到正确的房间。他们甚至还可以在服务器端接收事件。
当我尝试从其中一条路线发出事件时,会发生问题。整个应用程序可以在localhost上完美运行,没有“遗漏”事件,但是当我上载到flex GAE服务器时,有时仅从服务器接收事件。我打开了会话亲缘关系,我怀疑路由中的io对象没有“看到”套接字,因为它在不同的实例中,但是我不知道如何解决这个问题。
我使用具有以下配置的Swift Socket IO客户端启动并运行了iOS应用:
guard let token = token else { return }
let isDevEnvironment = Bundle.main.bundleIdentifier?.contains("dev") ?? false
self.manager.config = [.log(true),.compress,.reconnects(true),.reconnectWait(5),.forceWebsockets(true),.cookies(Alamofire.Sessionmanager.default.session.configuration.httpCookieStorage?.cookies! ?? []),.secure(!isDevEnvironment),.connectParams(["token": token,"collection_id": collectionId])]
self.socket.on("joined_room")
{
(data,ack) in
if let json = data.first as? [String : Any],let roomId = json["room_id"] as? String,let socketId = json["socket_id"] as? String
{
print("Connected to room with collection ID \(roomId) and ID \(socketId)")
}
}
self.socket.on(clientEvent: .reconnectAttempt)
{
(data,ack) in
print("Socket attempting to reconnect to server")
self.manager.disconnect()
self.socket.connect()
}
self.socket.on(clientEvent: .disconnect)
{
(data,ack) in
print("Socket disconnected")
}
我的服务器端配置如下
const app = express();
const server = Server(app);
const io = socketIO(server,{ transports: ['websocket'],cookie: true,secure: true });
io.use(async (socket,next) => {
if (socket.handshake.query && socket.handshake.query.token) {
try {
const decodedToken = await admin.auth().verifyIdToken(socket.handshake.query.token);
const { uid } = decodedToken;
if (uid === undefined || uid === null) {
throw new Error('This token is unauthorized or no user is associated to this token');
}
next();
} catch (error) {
next(error);
}
}
}).on('connection',socket => {
socket.join(socket.handshake.query.collection_id,err => {
if (err) {
console.log(err);
}
console.log(`Socket with ID ${socket.id} joined room with ID ${socket.handshake.query.collection_id}`);
});
socket.on('disconnect',() => {
console.log(`Socket with ID ${socket.id} disconnected`);
});
});
app.set('io',io);
要开除我,只需使用app.get('io').to(roomId).emit(event,object);
我是否需要做一些事情,以便有适当的路由/负载平衡,而不仅仅是打开会话亲和力?