Skip to content

Commit

Permalink
show admin broadcast
Browse files Browse the repository at this point in the history
  • Loading branch information
x3388638 committed Apr 16, 2017
1 parent 0033a6e commit 8a9f8e6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
13 changes: 7 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ io.on('connection', function(socket){
* API handler
*/
app.post('/register', cors(), function (req, res) {
let ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
_serverIP = ip.replace('::ffff:', '');
let key = req.body.key;
let selfIP = req.body.ip;
let name = req.body.name;
if (key != mcast.getKey()) {
res.json({
err: 'permission denied'
});
return;
}
let ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
_serverIP = ip.replace('::ffff:', '');
let selfIP = req.body.ip;
let name = req.body.name;
mcast.resetKey();
io.sockets.emit('register', JSON.stringify({
data: {
Expand All @@ -70,14 +70,15 @@ app.post('/message', cors(), function (req, res) {
res.send('access denied');
return;
}

io.sockets.emit('message', JSON.stringify({
event: 'newMessage',
data: {
msg: req.body.msg,
ip: req.body.ip,
name: req.body.name,
time: req.body.time
time: req.body.time,
admin: req.body.admin
}
}));

Expand Down
2 changes: 1 addition & 1 deletion web/js/ChatScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var ChatScreen = (_ => {
function renderMsg(type, msgObj) {
let $container = type == 'history' ? $chatContainer.find('#history') : $chatContainer.find('#current');
$container.append(
`<div class="msgRow">
`<div class="msgRow ${msgObj.admin ? 'admin' : ''}">
<div class="title">
<span class="name">${msgObj.name}</span><br />
<span class="ip">${msgObj.ip}</span>
Expand Down
7 changes: 4 additions & 3 deletions web/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var App = (_ => {
});

function _handleOnRegister(msg) {
console.log(`===== receive from local server: ${msg} =====`);
// console.log(`===== receive from local server: ${msg} =====`);
msg = JSON.parse(msg);
if(msg.data.register) {
console.log('register success.');
Expand All @@ -39,14 +39,15 @@ var App = (_ => {

function _handleOnMessage(data) {
data = JSON.parse(data);
console.log(`===== receive msg from ${data.data.ip} =====`);
// console.log(`===== receive msg from ${data.data.ip} =====`);
switch(data.event) {
case 'newMessage':
ChatScreen.renderMsg('current', {
msg: data.data.msg,
ip: data.data.ip,
name: data.data.name,
time: data.data.time
time: data.data.time,
admin: data.data.admin
});
break;
}
Expand Down

0 comments on commit 8a9f8e6

Please sign in to comment.