Nodejs instant chat

Source: Internet
Author: User
Tags emit

Nodejs instant chat

I always wanted to create an instant chat application. I saw socket. io a few days ago. I felt pretty good. I made some modifications myself and it felt quite good. The example given on the official website is very simple. The following improvements have been made to push historical messages.

Demo address: chat.androiddevelop.cn


Server code:

Var app = require ('express ') (); var http = require ('http '). server (app); var io = require ('socket. io ') (http); var history = new Array (); app. get ('/', function (req, res) {res.sendfile('chat.html ') ;}); io. on ('connection', function (socket) {socket. on ('chat message', function (msg) {io. emit ('chat message', msg); addMsg (msg) ;}); socket. on ('login message', function (msg) {socket. join ('History room'); for (var I = 0; I
 
  
100) history. shift ();};
 


Chat page code:

      Chat Room            
 
 
    <Script src = "/socket. io/socket. io. js "> </script> <script src =" http://code.jquery.com/jquery-1.11.1.js "> </script> <script> var socket = io (); var login = true; var username = ""; var myDate = new Date (); $ ('form '). submit (function () {if (login) {username = $ ('# m '). val (); if (username. length = 0) {alert ("Enter the user name"); return false;} login = false; socket. emit ('login message', "lyd _" + username + 'added to the chatting room' + myDate. getMonth () + "-" + myDate. getDate () + "" + myDate. getHours () + ":" + myDate. getMinutes () + ":" + myDate. getSeconds (); ('{btn'{.html ("send");} else {socket. emit ('chat message', username + "#" + $ ('# m '). val () ;}$ ('# m '). val (''); return false ;}); socket. on ('chat message', function (msg) {var item = msg. split ("#", 2); if (msg. indexOf ('lyd _ ') = 0) $ (' # messages '). append ('
  • '+ Msg. substr (5) +'
  • '); Else if (msg. indexOf (username) = 0) {$ (' # messages '). append ('
  • '+ Item [0] + ':
  • '); $ (' # Messages '). append ('
  • '+ Item [1] +'
  • ');} Else {$ (' # messages '). append ('
  • '+ Item [0] + ':
  • '); $ (' # Messages '). append ('
  • '+ Item [1] +'
  • ') ;}}); </Script>

    In this way, a chat room is implemented. Enter the user name, log on, and the server returns the last 100 messages.

    Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.