-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPoly.js
More file actions
132 lines (112 loc) · 3.11 KB
/
Poly.js
File metadata and controls
132 lines (112 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import Lobby from './Lobby.js'
import Peer from './Peer.js'
const log = console.log
// const POLY_SERVER_URI = 'ws://localhost:3000'
const POLY_SERVER_URI = 'ws://ec2-18-233-160-38.compute-1.amazonaws.com:3000'
class Poly {
constructor(){
this.lobby = null
this.lobbies = []
this.me = null
this.peers = []
}
connect(onConnected){
this.socket = new WebSocket(POLY_SERVER_URI)
this.socket.addEventListener('open', onConnected)
this.socket.addEventListener('message', this.onMessage.bind(this))
}
onMessage(event){
let data = JSON.parse(event.data)
this[data.method](event, data)
}
// Events
init(event, data){
this.lobbies = data.lobbies.map(lobby => JSON.parse(lobby))
this.onInit()
}
lobbyAdded(event, data){
let lobby = JSON.parse(data.lobby)
this.lobbies.push(lobby)
this.onLobbyAdded(lobby)
}
lobbyUpdated(event, data){
this.lobby = data.lobby
this.onLobbyUpdated(data.lobby)
}
// lobbiesUpdated(event, data){
// log("ON LOBIES UPDATED", data)
// this.lobbies = data.lobbies.map(lobby => JSON.parse(lobby))
// this.onLobbiesUpdated(this.lobbies)
// }
// When a new Peer Joins a Lobby, update the local Lobby, if the Peer joined your Lobby connect
async peerJoined(event, data){
let peer = data.peer
let lobby = this.lobbies.find(lobby => lobby.id == data.lobby.id)
lobby.peers.push(data.peer)
if(this.lobby && lobby.id == this.lobby.id){
log("peerJoined", data.peer)
let desc = await this.me.onPeerJoined(peer)
this.socket.send(JSON.stringify({
method: 'peerBAnswer',
to: peer.id,
from: this.me.serialize(),
desc: desc,
}))
}
}
async peerBAnswer(event, data){
let to = data.to
let from = data.from
let desc = data.desc
if(this.me && to == this.me.id){
await this.me.onPeerBAnswer(from, desc)
}
}
async peerCandidate(event, data){
let to = data.to
let from = data.from
let candidate = data.candidate
if(this.me && to == this.me.id){
log("peerCandidate: ", data)
await this.me.onPeerICECandidate(from, candidate)
}
}
// Methods
host(){
this.lobby = new Lobby()
this.me = new Peer(this.lobby, null, this.socket)
this.lobby.join(this.me)
this.lobbies.push(this.lobby)
this.socket.send(JSON.stringify({
method: 'addLobby',
lobby: this.lobby.serialize()
}))
this.onLobbyAdded(this.lobby)
}
async join(lobby){
this.lobby = new Lobby(lobby.id, lobby.peers)
this.me = new Peer(this.lobby, null, this.socket)
return this.me.initConnections().then(() => {
log("join: me", this.me)
this.lobby.join(this.me)
this.socket.send(JSON.stringify({
method: 'joinLobby',
lobby_id: this.lobby.id,
peer: this.me.serialize(),
}))
})
}
close(){
this.lobbies = []
this.socket.send(JSON.stringify({
method: 'closeLobbies',
}))
this.onLobbiesUpdated(this.lobbies)
}
// Override - Events
onInit(){}
onLobbyUpdated(){}
onLobbyAdded(){}
onLobbiesUpdated(){}
}
export default new Poly()