-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add get_Inline users feature and seve every socket
- Loading branch information
0 parents
commit 95b2765
Showing
18 changed files
with
17,900 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/node_modules | ||
/package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# chat |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* chat created by ZJH9Rondo | ||
* 21/07/2017 | ||
*/ | ||
const express = require('express'); | ||
const path = require('path'); | ||
const app = express(); | ||
const server = require('http').createServer(app); | ||
const io = require('socket.io').listen(server); | ||
const routes = require('./routes/connect'); | ||
const sockets = []; | ||
|
||
// view engine setup | ||
app.set('views', path.join(__dirname, 'views')); | ||
app.set('view engine', 'jade'); // 模板引擎 jade | ||
|
||
// 静态文件目录 | ||
app.use(express.static(path.join(__dirname,'public'))); | ||
|
||
|
||
// socket | ||
io.on('connection',(socket) => { | ||
socket.on('message',(data) => { | ||
let flag = 0; | ||
if(sockets.length === 0){ | ||
sockets.push({ | ||
"socket": socket, | ||
"name": data.chat_with, | ||
}); | ||
sockets[0].socket.emit('message',data.to); | ||
console.log(sockets); | ||
return; | ||
} | ||
|
||
for(let i in sockets){ | ||
if(data.chat_with === sockets[i].name){ | ||
if(sockets[i].socket !== socket){ | ||
sockets[i].socket = socket; | ||
} | ||
sockets[i].socket.emit('message',data.chachat_with); | ||
flag = 1; | ||
return; | ||
} | ||
} | ||
|
||
if(flag === 0){ | ||
sockets.push({ | ||
"socket": socket, | ||
"name": data.chat_with | ||
}); | ||
socket.emit('message',data.chat_with); | ||
} | ||
console.log(sockets); | ||
}); | ||
}); | ||
|
||
// 路由信息 | ||
app.get('/',(req,res,next) => { | ||
res.render('index'); | ||
}); | ||
routes(app,sockets); | ||
|
||
server.listen(3000); | ||
|
||
module.exports = app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const gulp = require('gulp'); | ||
const uglify = require('gulp-uglify'); | ||
const livereload = require('gulp-livereload'); | ||
|
||
gulp.task('uglify',() => { | ||
gulp.src(['./public/javascripts/*.js']) | ||
.pipe(uglify()) | ||
.pipe(gulp.dest('./public/javascripts')); | ||
}); | ||
|
||
gulp.watch('./public/javascripts/*.js',['uglify']); | ||
|
||
gulp.task('default',[ | ||
'uglify', | ||
'watch', | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "chat", | ||
"version": "0.0.1", | ||
"private": true, | ||
"main": "app.js", | ||
"scripts": { | ||
"start": "node app" | ||
}, | ||
"dependencies": { | ||
"express": "^4.15.3", | ||
"gulp": "^3.9.1", | ||
"gulp-livereload": "^3.8.1", | ||
"gulp-uglify": "^3.0.0", | ||
"jade": "^1.11.0", | ||
"jquery-weui": "^1.0.1", | ||
"path": "^0.12.7", | ||
"socket.io": "^1.7.4" | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
;(() =>{ | ||
const message_ctn = document.getElementsByClassName('message-ctn'); | ||
const user_name = document.getElementsByClassName('user-name'); | ||
|
||
// signup遮罩层 | ||
(() => { | ||
const cover_main = document.getElementById('cover-main'); | ||
const nick = document.getElementById('nick'); | ||
const signup = document.getElementById('signup'); | ||
|
||
// 获取在线好友 | ||
function get_Inline(sockets){ | ||
console.log(sockets.length); | ||
for(let i in sockets){ | ||
let message = document.createElement('div'); | ||
|
||
message.className = 'message'; | ||
message_ctn[0].appendChild(message); | ||
message_ctn[0].lastChild.innerHTML = '<div class="user-front"><img src="" alt=""></div><div class="message-dsr"><p class="user-name">' + sockets[i].name + '</p><p class="message-text"></p></div>'; | ||
} | ||
} | ||
|
||
// addEventListener | ||
function select_chat(){ | ||
let messages = document.getElementsByClassName('message'); | ||
|
||
for(let i = 0;i < messages.length; i++){ | ||
messages[i].addEventListener('click',() => { | ||
$.ajax({ | ||
type: "get", | ||
url: "http://127.0.0.1:3000/connect", | ||
datatype: "JSON", | ||
success: (data) => { | ||
|
||
data = JSON.parse(data); | ||
window.open(data.redirect); | ||
window.addEventListener('message',(e) => { | ||
if(e.origin == 'http://127.0.0.1:3000'){ | ||
switch(e.data){ | ||
case 'ready': interval = setTimeout((win) => { | ||
|
||
win.postMessage(user_name[i].innerText,'http://127.0.0.1:3000/chat'); | ||
},1000,e.source); | ||
break; | ||
case 'closed': clearInterval(interval); | ||
break; | ||
} | ||
} | ||
},false); | ||
}, | ||
}); | ||
},false); | ||
} | ||
} | ||
|
||
signup.addEventListener('click',(e) => { | ||
let socket = io.connect(); | ||
|
||
socket.emit('message',{"chat_with": nick.value}); | ||
cover_main.style.display = 'none'; | ||
|
||
$.ajax({ | ||
type: "get", | ||
url: "http://127.0.0.1:3000/get_Inline", | ||
datatype: "JSON", | ||
success: (sockets) => { | ||
sockets = JSON.parse(sockets); | ||
get_Inline(sockets); | ||
select_chat(); | ||
} | ||
}); | ||
}); | ||
})(); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* create socket connect | ||
*/ | ||
(() => { | ||
const send = document.getElementById('send'); | ||
const message = document.getElementById('message'); | ||
const socket = io.connect(); // 建立socket连接 | ||
|
||
let chat_name; | ||
|
||
/* | ||
* | ||
*/ | ||
(() => { | ||
window.addEventListener('message',(e) => { | ||
chat_name = e.data; | ||
},false); | ||
|
||
// 当文档加载完毕, 给父级来源发送信息。 | ||
window.addEventListener('load', (e) => { | ||
e.currentTarget.opener.postMessage('ready','http://127.0.0.1:3000'); | ||
}, false); | ||
})(); | ||
|
||
/* | ||
* chat | ||
*/ | ||
(() => { | ||
// send message | ||
send.addEventListener("click", () => { | ||
let message_self = document.createElement('p'), | ||
message_ctn = document.getElementById('message-ctn'), | ||
messageObj; | ||
|
||
socket.emit('message',{"message": message.value,"chat_with": chat_name}); | ||
message_self.className = 'message_self'; | ||
message_self.innerText = message.value; | ||
message_ctn.appendChild(message_self); | ||
}); | ||
|
||
// accept and display | ||
socket.on('message',(data) => { | ||
let message_res = document.createElement('p'), | ||
message_ctn = document.getElementById('message-ctn'); | ||
|
||
message_res.className = 'message_res'; | ||
message_res.innerText = data; | ||
message_ctn.appendChild(message_res); | ||
}); | ||
})(); | ||
})(); |
Oops, something went wrong.