Skip to content

Commit

Permalink
双向通信
Browse files Browse the repository at this point in the history
  • Loading branch information
ksky521 committed Dec 1, 2015
1 parent 0976512 commit 1d14222
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 113 deletions.
71 changes: 54 additions & 17 deletions assets/js/nodeppt.control.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* 控制端函数
*/
(function($win, $doc, $B, Slide, loadJS, undefined) {
(function ($win, $doc, $B, Slide, loadJS, undefined) {
var $slides = Slide.$slides;

// function doItem(direction) {
Expand All @@ -15,19 +15,19 @@
var Control = {
state: 'unbind',
methods: {},
init: function() {
init: function () {
this.bindListener();
},
bindListener: function() {
bindListener: function () {
var t = this;
//监听用户端发出的广播
$B.on('nodepptEvent:eventKeyup', function(e) {
$B.on('nodepptEvent:eventKeyup', function (e) {
t.sendKeyEvent(e.keyCode);
}).on('nodepptEvent:show paint', function(e) {
}).on('nodepptEvent:show paint', function (e) {
t.sendKeyEvent(80);
}).on('nodepptEvent:remove paint', function() {
}).on('nodepptEvent:remove paint', function () {
t.sendKeyEvent(67);
}).on('nodepptEvent:paint points', function(points) {
}).on('nodepptEvent:paint points', function (points) {
var data = {
points: points,
screen: {
Expand All @@ -38,28 +38,30 @@
t.send_('default', ['paint points', data]);
})
//监听控制来的广播
.on('controlEvent:proxyFn', function(json) {
.on('controlEvent:proxyFn', function (json) {
var fnName = json.fnName;
var args = json.args;
try{
try {
args = JSON.parser(args);
}catch(e){
} catch (e) {
args = '';
}
Slide.proxyFn(fnName, args);
}).on('controlEvent:keyEvent', function(json) {
}).on('controlEvent:keyEvent', function (json) {
t.createKeyEvent_(json.keyCode);
}).on('controlEvent:syncStatus', function (json) {
Slide.setIndex(json.id, json.item);
});
},
createKeyEvent_: function(keyCode) {
createKeyEvent_: function (keyCode) {
var evt = document.createEvent('Event');
evt.initEvent('keyup', true, true);
evt.keyCode = keyCode;
evt.isFromControl = true;

document.dispatchEvent(evt);
},
send_: function(fnName, args) {
send_: function (fnName, args) {
var methods = this.methods;
var method;
args = getType(args) === 'Array' ? args : [args];
Expand All @@ -69,12 +71,12 @@
typeof method === 'function' && method.apply(Slide, args);
}
},
sendKeyEvent: function(keycode) {
sendKeyEvent: function (keycode) {
this.send_('keyEvent', [keycode]);
},

//添加一个新的监控
add: function(name, factory, override) {
add: function (name, factory, override) {
var methods = this.methods;

if (override || !methods[name]) {
Expand All @@ -85,13 +87,48 @@
//keyEvent;
}
},
load: function(type, args) {
load: function (type, args) {
var url = Slide.dir + 'nodeppt.control.' + type + '.js';
loadJS(url, function() {
loadJS(url, function () {
Slide.Control.methods[type].init(args);
});
}
};
Control.init();
Slide.Control = Control;

Slide.timerCtrl = timerCtrl;

function timerCtrl() {
var $body = document.body;
if ($body.offsetWidth / window.devicePixelRatio < 640) {
//太小的屏幕不要显示下一页了
return;
}
$body.classList.add('popup');
$body.classList.add('with-notes');
var $timer = document.createElement('time');
$timer.id = '_timer_';
$body.appendChild($timer);
var hour = 0,
sec = 0,
min = 0;
timer2 = setInterval(function () {
sec++;
if (sec === 60) {
sec = 0;
min++;
}
if (min === 60) {
min = 0;
hour++;
}
$timer.innerHTML = ['时间:' + time2str(hour), time2str(min), time2str(sec) + ' 幻灯片:' + Slide.current + '/' + Slide.count].join(':');
}, 1000);
}

function time2str(time) {
time = '00' + time;
return time.substr(-2);
}
}(window, document, MixJS.event.broadcast, Slide, MixJS.loadJS));
35 changes: 10 additions & 25 deletions assets/js/nodeppt.control.postMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ Slide.Control.add('postMessage', function (S, broadcast) {
});
return back;
}
broadcast.on('controlEvent:joinClient', function () {
postMSG.send_default('syncStatus', {
id: S.current,
item: S.curItem
});
});

var postWin, popup, timer;
var postMSG = {
Expand All @@ -31,6 +37,7 @@ Slide.Control.add('postMessage', function (S, broadcast) {
// console.log('client 发来贺电', arguments);
// },
evtHandler: function (e) {
var data = e.data;
broadcast.fire(data.action, data.data);
},
closeClient: function () {
Expand Down Expand Up @@ -66,36 +73,14 @@ Slide.Control.add('postMessage', function (S, broadcast) {
args: args
});
};
var $body = document.body;
$body.classList.add('popup');
$body.classList.add('with-notes');
var $timer = document.createElement('time');
$timer.id = '_timer_';
$body.appendChild($timer);
var hour = 0,
sec = 0,
min = 0;
timer2 = setInterval(function () {
sec++;
if (sec === 60) {
sec = 0;
min++;
}
if (min === 60) {
min = 0;
hour++;
}
$timer.innerHTML = ['时间:' + time2str(hour), time2str(min), time2str(sec) + ' 幻灯片:' + Slide.current + '/' + Slide.count].join(':');
}, 1000);
Slide.timerCtrl();
postWin = window.opener;
postMSG.send_default('joinClient');
window.addEventListener('message', this.evtHandler, true);
}
}
};

function time2str(time) {
time = '00' + time;
return time.substr(-2);
}

return postMSG;
});
32 changes: 6 additions & 26 deletions assets/js/nodeppt.control.socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ Slide.Control.add('socket', function (S, broadcast) {
keyCode: keyCode
})
},
clientConnect: function () {},
clientConnect: function () {
},
evtHandler: function () {
//角色是client,即被控制端,则连控制端服务器
webSocket.on('transfer.data', function (data) {
Expand Down Expand Up @@ -101,10 +102,9 @@ Slide.Control.add('socket', function (S, broadcast) {
if (showQrcode && showQrcode.isShow) {
showQrcode();
}
webSocket.emit('transfer.data', {
action: 'syncStatus',
Socket.send_default('syncStatus', {
id: S.current,
item: S.buildItem
item: S.curItem
});
break;
case 'leave':
Expand All @@ -127,27 +127,7 @@ Slide.Control.add('socket', function (S, broadcast) {
//角色,是否为控制端
if (args.isControl) {
this.role = 'control';
var $body = document.body;
$body.classList.add('popup');
$body.classList.add('with-notes');
var $timer = document.createElement('time');
$timer.id = '_timer_';
$body.appendChild($timer);
var hour = 0,
sec = 0,
min = 0;
timer2 = setInterval(function () {
sec++;
if (sec === 60) {
sec = 0;
min++;
}
if (min === 60) {
min = 0;
hour++;
}
$timer.innerHTML = ['时间:' + time2str(hour), time2str(min), time2str(sec) + ' 幻灯片:' + Slide.current + '/' + Slide.count].join(':');
}, 1000);
Slide.timerCtrl();
} else {
this.role = 'client';
}
Expand All @@ -159,7 +139,7 @@ Slide.Control.add('socket', function (S, broadcast) {
var now = Date.now();
if (now - lastTime > 3000) {
lastTime = now;
Slide.next();
Socket.send_keyEvent(39);
}
}, true);

Expand Down
Loading

0 comments on commit 1d14222

Please sign in to comment.