码迷,mamicode.com
首页 > Web开发 > 详细

NodeJS + Socket.IO 最终版

时间:2016-04-17 11:27:53      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

服务器端

 

//socket.io
var app = require("express")();
var http = require("http").Server(app);
var io = require("socket.io")(http);

//couchbase
var couchbase = require("couchbase");
var cluster = new couchbase.Cluster("couchbase://ip");
var bucket = cluster.openBucket("name","pass");

var onlineUsers = {};
var onlineCount = 0;

io.on("connection" , function(socket){

	socket.on("login" , function(obj){
		socket.name = obj.userid;
		console.log(new Date() + " : 编号" + obj.userid + "员工已登录!");

		if(onlineUsers.hasOwnProperty(obj.userid)){
			delete onlineUsers[obj.userid];
			onlineCount --;
		}

		onlineUsers[obj.userid] = {‘userid‘:obj.userid,‘socket‘:socket};
		onlineCount ++;
	});

	socket.on("disconnect" , function(){
		if(onlineUsers.hasOwnProperty(socket.name)){
			var obj = {userid:socket.name};
			delete onlineUsers[socket.name];
			onlineCount --;

			console.log(new Date() + " : 编号" + obj.userid+‘已登出!‘);
		}
	});

});


setInterval(function(){	
	bucket.getMulti([ "Document1" , "Document2" ], function(err, result) {
		if(err){
			console.log(new Date() + " : ERROR : " + err);
		}
		else
		{
			NodeEmit(result);
		}
	});		

} , 10 * 1000);

function NodeEmit(result){
	for(var user in onlineUsers){
			// Document1
			_Str_cmd1 = result["Document1"].value["content"][user];
			onlineUsers[user].socket.emit("cmd1" , _Str_cmd1);
			
			// Document2
			if(typeof(result["Document2"].value["content"][user]) != "undefined"){
				_Str_cmd2 = JSON.stringify(result["Document2"].value["content"][user]);
				onlineUsers[user].socket.emit("cmd2" , _Str_cmd2);
			}
			
		}
}

http.listen(8066 , function(){
	console.log("listening on *:8066");	
});

  

客户端 JS

function GetNoticeTipCount() {
    if ($("#hfAdminID").val() != "") {
        var adminid = $("#hfAdminID").val();

        //连接
        var socket = io.connect("ws://ip:8066/", {
            ‘forceNew‘: true,
            ‘reconnect‘: true
        });
        socket.emit("login", { userid: adminid });
        InitNotice(socket);

        /*********************************/
		// 自动重连,记录状态
        socket.on(‘connect‘, function(data) {    
            console.log(moment().format(‘HH:mm:ss‘) + " 已连接服务器");
			//重连成功后重新提交登录请求
            socket.emit("login", { userid: adminid });
        });  
        socket.on(‘connect_failed‘, function(data) {  
            console.log(moment().format(‘HH:mm:ss‘) + " 连接失败");
        });  
        socket.on(‘error‘, function(data) {  
            console.log(moment().format(‘HH:mm:ss‘) + " ERROR");
        });  
        socket.on(‘reconnecting‘, function (data) {  
            console.log(moment().format(‘HH:mm:ss‘) + " 重连服务器中");
        });  
        socket.on(‘reconnect‘, function (data) {  
            console.log(moment().format(‘HH:mm:ss‘) + " 重连服务器成功");
        });  
        socket.on(‘disconnect‘, function (data) {  
            console.log(moment().format(‘HH:mm:ss‘) + " 连接断开");
        });  

        /***********************************/

    }
}

//初始化提醒
function InitNotice(socket) {
    socket.on("cmd1", function (_notices) {
        console.log(moment().format(‘HH:mm:ss‘) + " " + _notices);
		//其他操作
    });

    socket.on("cmd2", function (_cnc) {
        console.log(moment().format(‘HH:mm:ss‘) + " " + _cnc);
		//其他操作
    });
}

  

 

NodeJS + Socket.IO 最终版

标签:

原文地址:http://www.cnblogs.com/chenliang-zibo/p/5400396.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!