Socket.io+angular.js+express.js do a chat application (iv)

Source: Internet
Author: User
Tags emit

Then the previous article

Building a chat room client using Angularjs


<!doctype html>

Change node. js

Angular.module (' justchatting ', []) angular.module (' justchatting '). Factory (' socket ', function ($rootScope) {var  Socket = Io.connect ('/') return {on:function (EventName, callback) {Socket.on (EventName, function () {var args = arguments $rootScope. $apply (function () {callback.apply (Soc Ket, Args)})}, Emit:function (EventName, data, callback) {socket.                    Emit (eventName, data, function () {var args = arguments $rootScope. $apply (function () {            if (callback) {callback.apply (socket, args)}}) })}}) Angular.module (' justchatting '). Directive (' Ctrlenterbreakline ', function () {return function (scope, element, Attrs)                {var Ctrldown = False Element.bind ("KeyDown", function (evt) {if (Evt.which = = = 17) { CtrlDown = True SetTimeout (function () {Ctrldown = false}, 1000)                if (Evt.which = = =) {if (Ctrldown) {Element.val (Element.val () + ' \ n ') } else {scope. $apply (function () {scope. $eval (Attrs.ctrlenterbrea                    KLine);                    });    Evt.preventdefault ()}}); };}); Angular.module (' justchatting '). Controller (' Messagecreatorctrl ', function ($scope, socket) {$scope. CreateMessage = function () {socket.emit (' messages.create ', $scope. newmessage) $scope. Newmessage = '}}) Angular.module (            ' justchatting '). Directive (' Autoscrolltobottom ', function () {return {link:function (scope, element, Attrs) {                Scope. $watch (function () {return Element.children (). length;    }, function () {                Element.animate ({scrollTop:element.prop (' scrollheight ')}, 100                0);        }            ); }    };}); Angular.module (' justchatting '). Controller (' Roomctrl ', function ($scope, socket) {$scope. messages = [] Socket.on (' mes  Sages.read ', function (messages) {$scope. Messages = messages}) Socket.on (' Messages.add ', function (message)    {$scope. Messages.push (Message)}) Socket.emit (' Messages.read ')})


A humble chat room was finished.


Project Source code address:Https://github.com/edagarli/chattingnode

Socket.io+angular.js+express.js do a chat application (iv)

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.