Skip to content

Commit 31cdd92

Browse files
committed
完成
1 parent af9d6a6 commit 31cdd92

File tree

4 files changed

+75
-54
lines changed

4 files changed

+75
-54
lines changed

lib/server.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module.exports.start = function(port) {
2121
sockets[uid] = socket;
2222
//监听添加map
2323
socket.on('add client', function(data) {
24-
var targetUid = socket.targetUid = data.tagetUid;
24+
var targetUid = socket.targetUid = data.targetUid;
2525
var anotherSocket = sockets[targetUid];
2626
userMap[targetUid] = uid;
2727
userMap[uid] = targetUid;
@@ -34,19 +34,18 @@ module.exports.start = function(port) {
3434
var uid = socket.uid;
3535
var targetUid = userMap[uid];
3636
var otherSocket = sockets[targetUid];
37-
otherSocket.emit('data from another client', data);
37+
console.log(data,uid,targetUid);
38+
otherSocket && otherSocket.emit('data from another client', data);
3839
});
3940

4041
//将当前分配的uid告知客户端
41-
socket.emit('UUID', {
42-
uid: uid
43-
});
42+
socket.emit('UUID', uid);
4443

4544
socket.on('disconnect', function() {
4645
var uid = socket.uid;
4746
var targetUid = userMap[uid];
4847
var otherSocket = sockets[targetUid];
49-
otherSocket.emit('system', {
48+
otherSocket && otherSocket.emit('system', {
5049
action: 'leave',
5150
leaveUid: uid
5251
});

ppts/demo.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ <h2>参考资料</h2>
7474
control:{
7575
type: 'socket',
7676
args:{
77-
isControl: location.hash === '#control',
78-
host: base + '',
77+
isControl: location.hash.slice(1,8) === 'control',
78+
host: base,
7979
shake: true
8080
}
8181
}

src/js/nodeppt.control.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
itemID--;
2020
}
2121
}
22-
function getType(obj){
23-
return ({}).toString.call(obj).slice(8, -1)
22+
23+
function getType(obj) {
24+
return ({}).toString.call(obj).slice(8, -1)
2425
}
2526

2627
var Control = {
@@ -46,7 +47,7 @@
4647
t.sendKeyEvent(e.keyCode);
4748
})
4849
//监听控制来的广播
49-
$B.on('from control order', function(json) {
50+
.on('from control order', function(json) {
5051
var fnName = json.fnName;
5152
var args = json.args;
5253
Slide.proxyFn(fnName, args);
@@ -57,7 +58,7 @@
5758
doItem(id, item);
5859
}).on('from control key event', function(keyCode) {
5960
t.createKeyEvent_(keyCode);
60-
})
61+
});
6162
},
6263
createKeyEvent_: function(keyCode) {
6364
var evt = document.createEvent('Event');
@@ -86,7 +87,7 @@
8687
sendKeyEvent: function(keycode) {
8788
this.send_('keyEvent', [keycode]);
8889
},
89-
90+
9091
//添加一个新的监控
9192
add: function(name, factory, override) {
9293
var methods = this.methods;

src/js/nodeppt.control.socket.js

+62-41
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
var socketIOURL = '//' + location.host + '/socket.io/socket.io.js';
2-
MixJS.loadJS(socketIOURL)
2+
MixJS.loadJS(socketIOURL);
3+
34
Slide.Control.add('socket', function(S, broadcast) {
5+
S.clientUID = 0;
46

57
var timer;
68
//检查是否加载完成socket.io.js
@@ -21,82 +23,101 @@ Slide.Control.add('socket', function(S, broadcast) {
2123
host: '',
2224
role: '', //角色
2325
clientConnect: function() {
24-
console.log(this.host + '/control');
2526
//角色是client,即被控制端,则连控制端服务器
26-
webSocket = io.connect(this.host + '/control');
27-
webSocket.on('system', function(data) {
28-
console.log(data);
29-
});
30-
webSocket.on('from control update', function(data) {
31-
broadcast.fire('from control update', data.id);
32-
});
33-
webSocket.on('from control updateItem', function(data) {
34-
broadcast.fire('from control updateItem', data.id, data.item);
35-
});
36-
webSocket.on('from control key event', function(data) {
37-
broadcast.fire('from control key event', data.keyCode);
38-
})
39-
webSocket.on('from control order', function(data) {
40-
var fnName = data.fn;
41-
var args = data.args;
42-
try {
43-
args = JSON.parse(args);
44-
} catch (e) {}
45-
Slide.proxyFn(fnName, args);
27+
28+
webSocket.on('data from another client', function(data) {
29+
var action = data.action;
30+
switch (action) {
31+
case 'from control update':
32+
broadcast.fire('from control update', data.id);
33+
break;
34+
case 'from control updateItem':
35+
broadcast.fire('from control updateItem', data.id, data.item);
36+
break;
37+
case 'from control key event':
38+
broadcast.fire('from control key event', data.keyCode);
39+
break;
40+
case 'from control order':
41+
var fnName = data.fn;
42+
var args = data.args;
43+
try {
44+
args = JSON.parse(args);
45+
} catch (e) {}
46+
Slide.proxyFn(fnName, args);
47+
break;
48+
}
4649
});
50+
4751
},
4852
controlConnect: function() {
53+
webSocket.emit('add client', {
54+
targetUid: this.clientUID
55+
});
4956

5057
//控制端不在直接运行函数,而是变成发送socket给client
5158
//注意参数,进行了json处理哦~
5259
Slide.proxyFn = function(fnName, args) {
5360
args = JSON.stringify(args);
54-
webSocket.emit('from control user order', {
61+
webSocket.emit('repost data', {
62+
action: 'from control order',
5563
fn: fnName,
5664
args: args
57-
});
65+
})
5866
}
5967
//角色是控制端,则连被控制端(client)服务器
60-
webSocket = io.connect(this.host + '/client');
61-
webSocket.on('system', function(data) {
62-
console.log(data);
63-
});
64-
webSocket.on('from client update', function(data) {
65-
broadcast.fire('from control update', data.id);
66-
});
67-
webSocket.on('from client updateItem', function(data) {
68-
broadcast.fire('from control updateItem', data.id, data.item);
68+
69+
webSocket.on('data from another client', function(data) {
70+
var action = data.action;
71+
switch (action) {
72+
case 'from client update':
73+
broadcast.fire('from control update', data.id);
74+
break;
75+
case 'from client updateItem':
76+
broadcast.fire('from control updateItem', data.id, data.item);
77+
break;
78+
79+
}
6980
});
70-
// webSocket.on('from client proxyFn', function(data) {
71-
// var fnName = data.fnName;
72-
// var args = data.args;
73-
// });
81+
7482
},
7583
connect: function() {
84+
webSocket = io.connect(this.host);
85+
webSocket.on('UUID', function(uid) {
86+
webSocket.uid = uid;
87+
});
88+
webSocket.on('system', function(data) {
89+
console.log(data);
90+
});
91+
7692
this[this.role + 'Connect']();
7793
},
7894
update: function(id) {
79-
webSocket.emit('from ' + Socket.role + ' user update', {
95+
webSocket.emit('repost data', {
96+
action: 'from ' + Socket.role + ' update',
8097
id: id
8198
});
8299
},
83100
updateItem: function(id, item) {
84-
webSocket.emit('from ' + Socket.role + ' user updateItem', {
101+
webSocket.emit('repost data', {
102+
action: 'from ' + Socket.role + ' updateItem',
85103
id: id,
86104
item: item
87105
});
88106
},
89107
keyEvent: function(keyCode) {
90-
webSocket.emit('from ' + Socket.role + ' user key event', {
108+
webSocket.emit('repost data', {
109+
action: 'from ' + Socket.role + ' key event',
91110
keyCode: keyCode
92111
});
93-
94112
},
95113

96114
init: function(args) {
97115
this.host = args.host || location.href;
116+
this.clientUID = location.hash.slice(8);
117+
console.log(this.clientUID);
98118
//角色,是否为控制端
99119
if (args.isControl) {
120+
console.log(this.clientUID);
100121
this.role = 'control';
101122
document.body.classList.add('popup');
102123
document.body.classList.add('with-notes');

0 commit comments

Comments
 (0)