Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/entities/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,11 @@ export default class Player {

await em.flush()

const conns = await socket.findConnectionsAsync(async (conn) => {
const conns = socket.findConnections((conn) => {
return (
conn.hasScope(APIKeyScope.READ_PLAYER_PRESENCE) &&
!!conn.playerAliasId &&
this.game.id === (await conn.getPlayerAlias())?.player.game.id
this.game.id === conn.game.id
)
})
await sendMessages(conns, 'v1.players.presence.updated', {
Expand Down
11 changes: 0 additions & 11 deletions src/socket/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,17 +182,6 @@ export default class Socket {
return Array.from(this.connections.values()).filter(filter)
}

async findConnectionsAsync(filter: (conn: SocketConnection) => Promise<boolean>): Promise<SocketConnection[]> {
const connections = Array.from(this.connections.values())
const results = await Promise.all(
connections.map(async (conn) => ({
conn,
matches: await filter(conn)
}))
)
return results.filter((r) => r.matches).map((r) => r.conn)
}

async trackEvent(data: Omit<SocketEventData, 'id'>): Promise<void> {
if (process.env.DISABLE_SOCKET_EVENTS === '1') {
return
Expand Down
2 changes: 1 addition & 1 deletion src/socket/listeners/gameChannelListeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const gameChannelListeners = [
id: data.channel.id,
game: conn.game
}, {
populate: ['members']
populate: ['members:ref']
})

if (!channel) {
Expand Down
8 changes: 6 additions & 2 deletions src/socket/messages/socketMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ export async function sendMessage<T extends object>(conn: SocketConnection, res:

export async function sendMessages<T extends object>(conns: SocketConnection[], type: SocketMessageResponse, data: T) {
await getSocketTracer().startActiveSpan('socket.send_many_messages', async (span) => {
await Promise.all(conns.map((conn) => conn.sendMessage(type, data)))
span.end()
try {
const message = JSON.stringify({ res: type, data })
await Promise.all(conns.map((conn) => conn.sendMessage(type, data, message)))
} finally {
span.end()
}
})
}
7 changes: 2 additions & 5 deletions src/socket/socketConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,11 @@ export default class SocketConnection {
return this.ticket.devBuild
}

async sendMessage<T extends object>(res: SocketMessageResponse, data: T): Promise<void> {
async sendMessage<T extends object>(res: SocketMessageResponse, data: T, serialisedMessage?: string): Promise<void> {
await getSocketTracer().startActiveSpan('socket.send_message', async (span) => {
if (this.ws.readyState === this.ws.OPEN) {
const devBuild = this.isDevBuild()
const message = JSON.stringify({
res,
data
})
const message = serialisedMessage ?? JSON.stringify({ res, data })

setTraceAttributes({
'socket.message_receiver.alias_id': this.playerAliasId,
Expand Down
Loading