• socket.io+angular.js+express.js做个聊天应用(四)


    接着上一篇

    使用angularjs构建聊天室的client


    <!doctype html>
    <html ng-app="justChatting">
    <head>
        <meta charset="UTF-8">
        <title>justChatting</title>
        <link rel="stylesheet" href="/bower_components/bootstrap/dist/css/bootstrap.min.css">
        <link rel="stylesheet" href="/stylesheets/room.css">
        <script type="text/javascript" src="/socket.io/socket.js"></script>
        <script type="text/javascript" src="/bower_components/jquery/dist/jquery.js"></script>
        <script type="text/javascript" src="/bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
        <script type="text/javascript" src="/bower_components/angular/angular.js"></script>
    </head>
    <body>
    <script type="text/javascript">
       var socket=io.connect('/');
        socket.on('connected',function(){
            alert('connected to justChatting!');
        });
    
    </script>
    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <a class="navbar-brand" href="#">justChatting</a>
            </div>
        </div>
    </div>
    <div class="container" style="margin-top:100px;">
        <div class="col-md-12">
            <div class="panel panel-default room" ng-controller="RoomCtrl">
                <div class="panel-heading room-header">justChatting</div>
                <div class="panel-body room-content">
                    <div class="list-group messages" auto-scroll-to-bottom>
                        <div class="list-group-item message" ng-repeat="message in messages">
                            某某: {{message}}
                        </div>
                    </div>
                    <form class="message-creator" ng-controller="MessageCreatorCtrl">
                        <div class="form-group">
                            <textarea required class="form-control message-input" ng-model="newMessage" ctrl-enter-break-line="createMessage()" placeholder="Ctrl+Enter to quick send"></textarea>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    <script type="text/javascript" src="javascripts/node.js"></script>
    </body>
    </html>


    改动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(socket, 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 === 13) {
                    if (ctrlDown) {
                        element.val(element.val() + '
    ')
                    } else {
                        scope.$apply(function() {
                            scope.$eval(attrs.ctrlEnterBreakLine);
                        });
                        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')
                        }, 1000);
                    }
                );
            }
        };
    });
    
    angular.module('justChatting').controller('RoomCtrl', function($scope, socket) {
        $scope.messages = []
        socket.on('messages.read', function (messages) {
            $scope.messages = messages
        })
        socket.on('messages.add', function (message) {
            $scope.messages.push(message)
        })
        socket.emit('messages.read')
    })


    一个简陋的聊天室完毕。


    项目源代码地址:https://github.com/edagarli/chattingnode

  • 相关阅读:
    阶段一-01.万丈高楼,地基首要-第2章 单体架构设计与准备工作-2-1 单体架构阶段概述与项目演示
    阶段一-01.万丈高楼,地基首要-第1章 学习指南-1-4 架构师所需要具备的技术栈与能力
    阶段一-01.万丈高楼,地基首要-第1章 学习指南-1-3 大型网站架构演变历程
    Spring cloud微服务安全实战-8-1课程总结
    Spring cloud微服务安全实战-7-13章节总结
    Spring cloud微服务安全实战-7-12整合链路追踪和日志监控
    Spring cloud微服务安全实战-7-11PinPoint+SpringBoot环境搭建
    Spring cloud微服务安全实战-7-10ELK日志采集架构优化
    Spring cloud微服务安全实战-7-9自定义日志采集的格式和内容
    Spring cloud微服务安全实战-7-8ELK+SpringBoot环境搭建
  • 原文地址:https://www.cnblogs.com/yxwkf/p/5179485.html
Copyright © 2020-2023  润新知