forked from petehunt/react-multiplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (62 loc) · 1.75 KB
/
index.js
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
io = require('socket.io-client');
Adapter = require('adapterjs');
Skylink = require('skylinkjs');
skylink = new Skyway();
//skylink.setLogLevel(skylink.LOG_LEVEL.DEBUG);
skylink.on('peerJoined', function(peerId, peerInfo, isSelf) {
if(!isSelf) {
console.log('peerJoined');
}
});
var DEFAULT_APPKEY = null;
var DEFAULT_ROOM = null;
var WebRTCSyncMixin = {
componentWillMount: function() {
this.updatingViaWebRTC = false;
},
componentDidMount: function() {
this._webRTCComponentId = window.btoa(this._rootNodeID + ',' + this._mountDepth);
skylink.on('incomingMessage', this.handleSkylinkMessage);
},
componentWillUnmount: function() {
skylink.off('incomingMessage', this.handleSkylinkMessage);
},
componentDidUpdate: function(prevProps, prevState) {
if (this.updatingViaWebRTC) {
return;
}
var update = {};
for (var k in this.state) {
if (prevState[k] !== this.state[k]) {
update[k] = this.state[k];
}
}
skylink.sendP2PMessage({
componentId: this._webRTCComponentId,
update: update
});
},
handleSkylinkMessageDone: function() {
this.updatingViaWebRTC = false;
},
handleSkylinkMessage: function(message, peerId, peerInfo, isSelf) {
if(!isSelf) {
var msg = message.content;
if(msg.componentId === this._webRTCComponentId) {
this.updatingViaWebRTC = true;
// TODO: can we remove this double reconcile?
this.replaceState(msg.update, this.handleSkylinkMessageDone);
}
}
}
};
module.exports = {
Mixin: WebRTCSyncMixin,
initSkylink: function(key, room) {
DEFAULT_APPKEY = key;
DEFAULT_ROOM = room || '';
skylink.init(DEFAULT_APPKEY, function() {
skylink.joinRoom(DEFAULT_ROOM);
});
}
};