Skip to content

Commit

Permalink
fix protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
ci010 committed Dec 21, 2023
1 parent b2fd885 commit 3c73b05
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
11 changes: 10 additions & 1 deletion xmcl-keystone-ui/src/composables/peerGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,23 @@ export class PeerGroup extends EventTarget {
// socket.onping = heartbeat)
socket.onmessage = (event) => {
const { data } = event
if (data instanceof Uint8Array) {
const onHeartbeat = (data: Uint8Array) => {
const id = [...data]
.map((b) => ('00' + b.toString(16)).slice(-2))
.join('')
.replace(/(.{8})(.{4})(.{4})(.{4})(.{12})/, '$1-$2-$3-$4-$5')
if (id !== this.id) {
this.onheartbeat?.(id)
}
}
if (data instanceof Blob) {
// Blob to Uint8Array
data.arrayBuffer().then(data => new Uint8Array(data))
.then(onHeartbeat)
return
}
if (data instanceof Uint8Array) {
onHeartbeat(data)
return
}
if (typeof data === 'string') {
Expand Down
3 changes: 2 additions & 1 deletion xmcl-keystone-ui/src/composables/peers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export function usePeerState(gameProfile: Ref<GameProfileAndTexture>) {
_group = new PeerGroup(groupId, _id)

_group.onheartbeat = (sender) => {
const peer = Object.values(connections.value).find(p => p.id === sender)
console.log(`Get heartbeat from ${sender}`)
const peer = connections.value.find(p => p.remoteId === sender)
// Ask sender to connect to me :)
if (!peer) {
if (_id.localeCompare(sender) > 0) {
Expand Down
8 changes: 8 additions & 0 deletions xmcl-runtime-api/src/services/PeerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface ConnectionUserInfo extends GameProfileAndTexture {
}
export interface PeerConnection {
id: string
remoteId: string
userInfo: ConnectionUserInfo
initiator: boolean
selectedCandidate?: {
Expand Down Expand Up @@ -67,6 +68,13 @@ export class PeerState {
}
}

connectionRemoteSet({ id, remoteId }: { id: string; remoteId: string }) {
const conn = this.connections.find(c => c.id === id)
if (conn) {
conn.remoteId = remoteId
}
}

connectionAdd(connection: PeerConnection) {
if (this.connections.find(c => c.id === connection.id)) {
return
Expand Down
4 changes: 3 additions & 1 deletion xmcl-runtime/peer/PeerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ export class PeerService extends StatefulService<PeerState> implements IPeerServ
})
}
}
const payload = { sdp, id, session, candidates }
const payload = { sdp, id: remoteId, session, candidates }
this.emit('connection-local-description', { type, description: payload })
pBrotliCompress(JSON.stringify(payload)).then((s) => s.toString('base64')).then((compressed) => {
this.state.connectionLocalDescription({ id: payload.session, description: compressed })
Expand Down Expand Up @@ -326,6 +326,7 @@ export class PeerService extends StatefulService<PeerState> implements IPeerServ
}, this, privatePort)

if (remoteId) {
this.state.connectionRemoteSet({ id: conn.id, remoteId })
conn.setRemoteId(remoteId)
}

Expand Down Expand Up @@ -360,6 +361,7 @@ export class PeerService extends StatefulService<PeerState> implements IPeerServ
this.state.connectionAdd({
id: conn.id,
initiator,
remoteId: remoteId ?? '',
userInfo: {
name: '',
id: '',
Expand Down

0 comments on commit 3c73b05

Please sign in to comment.