HTML5¼¼Êõ

websocketʵÏÖ¼òµ¥ÁÄÌì³ÌÐò - bingo¿§¿§

×ÖºÅ+ ×÷ÕߣºH5Ö®¼Ò À´Ô´£ºH5Ö®¼Ò 2017-09-13 08:09 ÎÒÒªÆÀÂÛ( )

³ÌÐòµÄÁ÷³Ìͼ£º Ö÷Òª´úÂ룺·þÎñ¶Ë app.js ÏȼÓÔØËùÐèÒªµÄͨÐÅÄ£¿é£º var express = require('express' ); var app = express(); var http = require('http' ).createServer(app); var io = require('socket.io' ).listen(http); var fs = require('fs'); ´´½¨

³ÌÐòµÄÁ÷³Ìͼ£º

Ö÷Òª´úÂ룺 ·þÎñ¶Ë app.js

ÏȼÓÔØËùÐèÒªµÄͨÐÅÄ£¿é£º

var express = require('express'); var app = express(); var http = require('http').createServer(app); var io = require('socket.io').listen(http); var fs = require('fs');

´´½¨Óû§ÁбíºÍÏûÏ¢ÁÐ±í£º

var person = []; var history = [];

°ó¶¨²¢¼àÌý80¶Ë¿Ú£º

app.get('http://www.cnblogs.com/',function (req,res) { res.sendFile(__dirname + '/login.html'); }); http.listen(80,function () { console.log('listening on *:80'); });

¿Í»§¶ËÁ¬½Ó³É¹¦ºó£¬´¥·¢ÏìӦʼþconnection£¬Íê³ÉÒª°ó¶¨µÄʼþ²¢ÊµÏÖ¿Í»§¶Ë³ö·¢µÄʼþ£º

io.sockets.on('connection',function (socket) { var user = ''; socket.emit('history', history); io.sockets.emit('updatePerson', person); socket.on('sendMsg', function (data) { var obj = {}; obj.content = data; obj.time = now(); obj.name = user; if (history.length === history_num) { history.shift(); } history.push(obj); io.sockets.emit('news', obj); }); socket.on('setUserName', function (data) { user = data; person.push(user); io.sockets.emit('loginsucess'); io.sockets.emit('updatePerson', person); io.sockets.emit('news', {content: user + '½øÈë·¿¼ä', time: now(), name: 'ϵͳÏûÏ¢'}); }); socket.on('disconnect', function () { if (user !== '') { person.forEach(function (value, index) { if (value === user) { person.splice(index, 1); } }); io.sockets.emit('news', {content: user + 'À뿪·¿¼ä', time: now(), name: 'ϵͳÏûÏ¢'}); io.sockets.emit('updatePerson', person); } }); });

¿Í»§¶Ë index.js£º

Ïȳõʼ»¯Óû§ÐÅÏ¢£º

$scope.data = []; //ÏûÏ¢¶ÓÁÐ $scope.name = ''; //Óû§Ãû $scope.content = ''; //Óû§ÊäÈëµÄÏûÏ¢ $scope.personlist = []; //Óû§¶ÓÁÐ

È»ºóÁ¬½Ó·þÎñÆ÷¶Ë£º

const socket_url = 'http://localhost'; var socket = io.connect(socket_url);

Á¬½Ó³É¹¦ºó£¬¶ÔÓû§êdzÆÊäÈëµÄÑéÖ¤£º

$scope.checkName = function () { if($scope.name!==''){ if($scope.personlist.length!==0){ if($scope.personlist.indexOf($scope.name)>-1) { document.getElementById("info").textContent = "¸ÃêdzÆÒѱ»Õ¼Óã¬ÇëÖØÐÂÊäÈë"; } else{ socket.emit('setUserName', $scope.name); } } else{ socket.emit('setUserName', $scope.name); } } else{ document.getElementById('name').focus(); } };

ÑéÖ¤³É¹¦ºó£¬¶ÔÓû§ÊäÈëÏûÏ¢Òª´¥·¢µÄʼþ£º

$scope.sendMsg = function(data){ var date = new Date(); data = $scope.content; if($scope.content !== ''){ socket.emit('sendMsg',data); } $scope.content = ''; };

³ÌÐòµÄ²¿·ÖÔËÐвâÊÔ½á¹û£º

ä¯ÀÀÆ÷ÊäÈëlocalhostºóչʾµÄÓû§µÇ¼½çÃæ:

êdzÆÖظ´ºóµÄÌáʾ£º

êdzÆÊäÈë³É¹¦ºó½øÈ뵱ǰÓû§µÄÁÄÌì½çÃ棺

 

Ô´ÂëµØÖ·£ºgithub

²Î¿¼Ñ§Ï°£º

Node.js + Web Socket ´òÔ켴ʱÁÄÌì³ÌÐòàËÁÄ

»ùÓÚwebsocketµÄÒ»¸ö¼òµ¥µÄÁÄÌìÊÒ

¡¡

1.±¾Õ¾×ñÑ­ÐÐÒµ¹æ·¶£¬ÈκÎתÔصĸå¼þ¶¼»áÃ÷È·±ê×¢×÷ÕߺÍÀ´Ô´£»2.±¾Õ¾µÄÔ­´´ÎÄÕ£¬ÇëתÔØʱÎñ±Ø×¢Ã÷ÎÄÕÂ×÷ÕߺÍÀ´Ô´£¬²»×ðÖØÔ­´´µÄÐÐΪÎÒÃǽ«×·¾¿ÔðÈΣ»3.×÷ÕßͶ¸å¿ÉÄܻᾭÎÒÃDZ༭Ð޸Ļò²¹³ä¡£

Ïà¹ØÎÄÕÂ
  • JsÀûÓÃCanvasʵÏÖͼƬѹËõ - similar

    JsÀûÓÃCanvasʵÏÖͼƬѹËõ - similar

    2017-09-12 12:00

  • jsÕ³Ìùʼþpaste¼òµ¥½âÎö¼°Óöµ½µÄ¿Ó - ÝíÀÙÐÄÇç

    jsÕ³Ìùʼþpaste¼òµ¥½âÎö¼°Óöµ½µÄ¿Ó - ÝíÀÙÐÄÇç

    2017-09-06 15:17

  • ÈçºÎʵÏÖÒ»¸öÂ¥ÖÐÂ¥µÄÆÀÂÛϵͳ - wenzi0_0

    ÈçºÎʵÏÖÒ»¸öÂ¥ÖÐÂ¥µÄÆÀÂÛϵͳ - wenzi0_0

    2017-09-01 18:00

  • css³¬¼òµ¥ÊµÏÖdivÒ³Ãæ¾ÓÖС¾ÊʺÏ×öµ¯³ö¿ò¡¿ - ËÕСËÕsxs

    css³¬¼òµ¥ÊµÏÖdivÒ³Ãæ¾ÓÖС¾ÊʺÏ×öµ¯³ö¿ò¡¿ - ËÕСËÕsxs

    2017-08-15 14:00

ÍøÓѵãÆÀ
«