本文整理汇总了TypeScript中socket.io.Socket.on方法的典型用法代码示例。如果您正苦于以下问题:TypeScript io.Socket.on方法的具体用法?TypeScript io.Socket.on怎么用?TypeScript io.Socket.on使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类socket.io.Socket
的用法示例。
在下文中一共展示了io.Socket.on方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1:
this._io.on("connection", (socket: Socket) => {
socket.on("register", (req: IRegisterRequest) => {
this.emitEvents(this._sessionManager.registerClient(socket, req));
});
socket.on("roomList", () => {
this.emitEvents(this._sessionManager.sendLobby(socket));
});
socket.on("joinRoom", (req: IJoinRoomRequest) => {
this.emitEvents(this._sessionManager.joinRoom(socket, req));
});
socket.on("leaveRoom", (req: ILeaveRoomRequest) => {
this.emitEvents(this._sessionManager.leaveRoom(socket, req));
});
socket.on("startGame", (req: IStartGameRequest) => {
this.emitEvents(this._sessionManager.startGame(socket, req));
});
socket.on("placeTile", (req: IPlaceTileRequest) => {
this.emitEvents(this._sessionManager.placeTile(socket, req));
});
socket.on("roomMessage", (req: IRoomMessageRequest) => {
this.emitEvents(this._sessionManager.roomMessage(socket, req));
});
socket.on("roomStats", (req: IRoomStatsRequest) => {
this.emitEvents(this._sessionManager.sendRoomStats(socket, req));
});
socket.on("userStats", (req: IUserStatsRequest) => {
this.emitEvents(this._sessionManager.sendUserStats(socket, req));
});
socket.on("globalStats", (async (req: IGlobalStatsRequest) => {
this.emitEvents(await this._sessionManager.sendGlobalStats(socket, req));
}));
});
示例2:
this.socket.on('connection', (client: Socket) => {
this.bus.addClient(client)
client.on('api.subscription', (subscription: Subscription) => {
this.bus.subscribe(client.id, subscription)
})
client.on('api.unsubscription', (subscription: Subscription) => {
this.bus.unsubscribe(client.id, subscription.id)
})
client.on('disconnect', () => {
this.bus.removeClient(client.id)
})
})
示例3: decode
io.on('connection', (socket: Socket) => {
const openWatchers = new Map<string, WatcherAndSubscribeHook>();
const unsubscribeWatcher = ({
watcher,
subscribeHook,
}: WatcherAndSubscribeHook) => {
watcher.stop();
if (subscribeHook) {
subscribeHook.unsubscribed(socket.user);
}
};
socket.on('disconnect', () => {
openWatchers.forEach(unsubscribeWatcher);
});
socket.on(
'resource',
(
{ type, resourceName, json }: EventResourceParams,
callback: Callback,
): void => {
try {
const value = json && decode(json);
switch (type) {
case 'cursor toArray': {
const resource = resourcesService.getCursorResource(resourceName);
resourcesService
.createCursor(resource, socket.user, value)
.then((cursor) => cursor.toArray())
.then((results) => callback(null, encode(results)))
.catch((err) => {
logger.error(type, err);
callback(err.message);
});
break;
}
case 'fetch':
case 'subscribe':
case 'fetchAndSubscribe':
try {
const resource = resourcesService.getServiceResource(
resourceName,
);
logger.info('resource', { type, resourceName, value });
const [key, params, eventName] = value;
if (!key.startsWith('query')) {
throw new Error('Invalid query key');
}
const query = resource.queries[key](params, socket.user);
if (type === 'fetch') {
query
.fetch((result: any) =>
callback(null, result && encode(result)),
)
.catch((err: any) => {
logger.error(type, { err });
callback(err.message || err);
});
} else {
const watcherKey = `${resourceName}__${key}`;
if (openWatchers.has(watcherKey)) {
logger.warn(
'Already have a watcher for this key. Cannot add a new one',
{ watcherKey, key },
);
callback(
'Already have a watcher for this key. Cannot add a new one',
);
return;
}
const watcher = query[type](
(err: Error | null, result: any) => {
if (err) {
logger.error(type, { err });
}
socket.emit(eventName, err, result && encode(result));
},
);
watcher.then(
() => callback(null),
(err: Error) => {
logger.error(type, { err });
callback(err.message);
},
);
const subscribeHook =
resource.subscribeHooks && resource.subscribeHooks[key];
openWatchers.set(watcherKey, { watcher, subscribeHook });
if (subscribeHook) {
//.........这里部分代码省略.........