Created
April 18, 2016 06:13
-
-
Save leapar/cc4c11541f1d10917a87215b10262f71 to your computer and use it in GitHub Desktop.
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
var app = require('express')(); | |
var fs = require('fs'); | |
//console.log(app); | |
var http = require('http') | |
var https = require('https'); | |
var io = require('socket.io'); | |
var bodyParser = require("body-parser"); | |
var opts = { | |
pfx: fs.readFileSync('pushServer.pfx'), | |
passphrase: '123456' | |
}; | |
var httpServer = http.createServer(app); | |
var httpsServer = https.createServer(opts, app); | |
io = new io(); | |
io.attach(httpServer); | |
io.attach(httpsServer); | |
//app.use(bodyParser.urlencoded({extended: false})); | |
app.use(bodyParser.json()) | |
/*app.get('/nyghome', function (req, res) { | |
res.send('<h1>Welcome Realtime Server</h1>'+onlineCount); | |
});*/ | |
//存储客户端连接信息 两级map | |
//userid => socket => object | |
var userMap = new Map(); | |
/* | |
{ | |
"content": "content", | |
"fromUser": "fromUser", | |
"toUser": "toUser", | |
"createTime": "createTime", | |
} | |
*/ | |
app.post('/push', function (req, res) { | |
console.log(req.ip); | |
var ret = {}; | |
ret.code = -1; | |
if (req.body == null || req.body.toUser == null) { | |
ret.error = "参数错误"; | |
console.log(req.body); | |
res.json(ret); | |
return; | |
} | |
var message = req.body; | |
if(userMap.has(req.body.toUser)) { | |
var clientMap = userMap.get(req.body.toUser); | |
clientMap.forEach(function(value, key, map) { | |
key.emit('message', message); | |
// console.log("Key: %s, Value: %s map:%s", key, value,map); | |
}); | |
} | |
var ret = {}; | |
ret.code = 0; | |
res.json(ret); | |
console.log(req.body); | |
}); | |
io.on('connection', function (socket) { | |
console.log(socket.request.headers.cookie); | |
// socket.disconnect(); | |
//监听新用户加入 | |
socket.on('login', function (clientInfo) { | |
console.log(clientInfo.userid); | |
// getUser(clientInfo.jsid); | |
//将新加入用户的唯一标识当作socket的名称,后面退出的时候会用到 | |
socket.userid = clientInfo.userid; | |
//检查在线列表,如果不在里面就加入 | |
var socketMap = null; | |
if (userMap.has(clientInfo.userid)) { | |
socketMap = userMap.get(clientInfo.userid); | |
} else { | |
socketMap = new Map(); | |
userMap.set(clientInfo.userid, socketMap); | |
} | |
socketMap.set(socket, {ip:socket.handshake.address, username:clientInfo.username}); | |
console.log( userMap.length); | |
//广播用户加入 | |
//io.emit('login', {onlineUsers: onlineUsers, onlineCount: onlineCount, user: obj}); | |
console.log(clientInfo.username+"连接"); | |
console.log(userMap); | |
socket.emit('login_success', {userid: clientInfo.userid}); | |
}); | |
//监听用户退出 | |
socket.on('disconnect', function () { | |
//将退出的用户从在线列表中删除 | |
if (userMap.has(socket.userid)) { | |
var socketMap = userMap.get(socket.userid); | |
var clientInfo = socketMap.get(socket); | |
socketMap.delete(socket); | |
console.log(clientInfo.username + '退出'); | |
} | |
}); | |
}); | |
httpServer.listen(3008, function () { | |
console.log('listening on *:3008'); | |
}); | |
httpsServer.listen(3009, function () { | |
console.log('listening on *:3009'); | |
}); | |
/*------------------create pfx-------------------------*/ | |
openssl.exe genrsa -out d://privatekey.pem 1024 | |
openssl.exe req -new -key d://privatekey.pem -out d://client.csr | |
这个是域名,其他随便,可以不填 | |
Common Name (e.g. server FQDN or YOUR name) []:pasysdev.cloudapp.net | |
openssl.exe x509 -req -in d://client.csr -signkey d://privatekey.pem -out d://certificate.pem | |
openssl.exe pkcs12 -export -in d://certificate.pem -inkey d://privatekey.pem -out d://pushServer.pfx |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment