2016-12-26 63 views
-1

我对Node.js的工作弹出聊天窗口,并开始创造我已经成功地建立了它一个聊天应用程序,但问题是,我想聊天窗口应该打开时,在客户端点击发送者的姓名(谁发送消息给客户端)。 我会告诉你我到现在为止所做的一切。如何使用Node.js的

enter image description here

在这里,您可以看到聊天框已经打开,但我想它应该打开时,从“用户列表”中选择用户和聊天框的内容应更换每当一个新用户被选中并且之前的内容应该被删除。 这里是我的index.html代码:

<div class="col-md-4 user-list"> 
      <h2>List of Users</h2> 
      <ul class="list-group"> 
       <li class="list-group-item" 
        ng-repeat="user in userList" 
        ng-class="{ 'active' : user.id == selectedUser.uid}" 
        ng-click = seletedUser(user.id,user.userName); 
        ng-style="{ 
         'cursor': user.id === socketId ? 'not-allowed' :'pointer' 
        }" 
        > 
        <span id='{{user.id}}' >{{ user.id === socketId ? 'You': user.userName }}</span> 
        <span id='{{user.id + "notify"}}' style="color:black; opacity:0.5; font:2px; padding:5px; visibility:hidden;"> {{'typing...'}}</span> 
       </li> 
      </ul> 

      </div> 
</div> 
      <div class="container" id="messages"> 
       <div class="row"> 
        <div class="col-md-8"> 
         <div class="panel panel-primary"> 
          <div class="panel-heading"> 
           <span class="glyphicon glyphicon-comment"></span> {{'Chat -' + selectedUser.uname}} 
          </div> 
          <div class="panel-body"> 
           <ul class="chat col-md-7" 
           ng-repeat = "message in messages" 
           ng-style ="{'float':message.fromid == socketId ? 'left' : 'right'}"> 
            <li> 
             <div class="clearfix"> 
              <div class="direct-chat-text" 
              ng-style = "{'background-color': message.fromid == socketId ? '#d9edf7' : '#62f3bc'}" 
              > 
               {{message.msg}} 

              </div> 

             </div> 
            </li> 
           </ul> 
           <br></br> 
          </div> 
          <div class="panel-footer"> 
           <div class="input-group"> 
            <textarea elastic type="text" class="form-control custom-control" ng-model='message' ng-keyup="keyup()" ng-keydown="keydown()" ng-change="change()" placeholder="Type your message here..."></textarea> 
            <span class="input-group-addon btn btn-primary" ng-click="sendMsg()"><i class="glyphicon glyphicon-send"></i></span> 
           </div> 
          </div> 
         </div> 
        </div> 
       </div> 
      </div> 

如果您需要相关代码的任何其他信息,请发表评论。 因为我是node.js的新手,所以帮助我解决我的问题。

UPDATE 我的script.js的代码,有足够的细节:

app.controller('app', ($scope,$timeout,socket) => { 

$scope.socketId = null; 
$scope.selectedUser ={ uid: null, uname: null}; 
$scope.messages = []; 
$scope.msgData = null; 
$scope.userList = []; 
var TypeTimer;     
var TypingInterval = 1000; 


$scope.username = window.prompt('Enter Your Name'); 
if ($scope.username === '') { 
    window.location.reload(); 

} 


$scope.seletedUser = (id,name) => { 
    if(id === $scope.socketId) 
    { 
     alert("Can't message to yourself.") 
    } 
    else 
    { 
     $scope.selectedUser = 
     { 
      uid: id, 
      uname: name 
     } 
    } 
}; 
    $scope.sendMsg =() => { 
// const keyCode = $event.which || $event.keyCode; 

    if ($scope.message !== null && $scope.message !== '' && $scope.selectedUser.uid !== null) {  
     socket.emit('getMsg',{ 
      toid : $scope.selectedUser.uid, 
      fromid: $scope.socketId, 
      msg : $scope.message, 
      name : $scope.username 
     }); 
     $timeout.cancel(TypeTimer); 
     TypeTimer=$timeout(function(){ 
     socket.emit('getTypingNotify',{ 
     toid : $scope.selectedUser.uid, 
     fromid: $scope.socketId, 
     msg:"hidden" 
      }); 
      }); 
     $scope.message = null; 
     console.log($scope.selectedUser.uid); 
    } 
    else 
    { 
     alert("enter a message or select a User to send message"); 
    } 
}; 



socket.emit('username',$scope.username); 

socket.on('userList', (userList,socketId) => { 
    if($scope.socketId === null){ 
     $scope.socketId = socketId; 
    } 
    $scope.userList = userList; 
});  


socket.on('exit', (userList) => { 
    $scope.userList = userList; 
}); 

socket.on('setMsg',(data) => { 
     document.getElementById(data.fromid+'notify').style.visibility= data.msg; 

     });  

socket.on('sendMsg', (data) => { 
    //console.log('send'); 
    $scope.messages.push(data); 
}); 
+0

请出示您的角度文件 – Codesingh

+0

你在显示事物的UI做的这样绝对没有与服务器端做。浏览器中的角运行,节点在服务器上运行。此外,这不是一个*“如何”*教程网站 – charlietfl

+0

我知道这不是一个教程网站。我已经提到,如果你需要代码,我会提供它。 – sagar

回答

0

在搜索了一些教程和问题(在StackOverflow上)之后,我找到了一种方法并与大家分享。

我创建了一个目录名称“open”和一个可见的“open”指令来显示和隐藏对话框。

app.directive('open',function() { 
return { 
    restrict: 'E', 
    template: `<div class="container"> 
       <div class="row"> 
        <div class="col-md-8"> 
         <div class="panel panel-primary"> 
          <div class="panel-heading"> 
           <span class="glyphicon glyphicon-comment"></span>{{"Chat -" + selectedUser.uname}} 
          </div> 
          <div class="panel-body"> 
           <div ppp-chat></div> 
           <br></br> 
          </div> 
          <div class="panel-footer"> 
           <div class="input-group"> 
            <textarea msd-elastic type="text" class="form-control custom-control" ng-model="message" ng-keyup="keyup()" ng-keydown="keydown()" ng-change="change()" placeholder="Type your message here..."></textarea> 
            <span class="input-group-addon btn btn-primary" ng-click="sendMsg()"><i class="glyphicon glyphicon-send"></i></span> 
           </div> 
          </div> 
         </div> 
        </div> 
       </div> 
      </div>`, 
     replace:true, 
     scope:false, 
     link: function postLink(scope,element,attrs) { 
      scope.$watch(attrs.visible,function(value){ 
       if(value == true) 
       { 
        $(element).show(); 
       } 
       else 
        $(element).hide(); 
      }); 
      $(element).on('show.bs.open',function() { 
       scope.$apply(function(){ 
        scope.$parent[attrs.visible] = true; 
       }); 
      }); 
      $(element).on('hidden.bs.open',function() { 
       scope.$apply(function(){ 
        scope.$parent[attrs.visible] = false; 
       }); 
      }); 
     } 
    }; 

});

而在控制器的方法来切换聊天窗口

$scope.toggleChat =() => { 
    $scope.showChat = false; 
    $scope.showChat = !$scope.showChat; 
}; 

此toggleChat用来改变被用来接通(关闭)showChat范围变量的值(隐藏或显示)的可见性通过将showChat值更改为true或false来添加聊天框。

在HTML我已经取代了我的ID =“消息”元素(其子和subchild)与

<open visible="showChat"> 
      </open> 
0

我的理解是ü要显示弹出按钮或一些文本框点击u可以使用角度bootstarp模型如下图所示

"https://plnkr.co/edit/?p=preview" 

和控制angularjs位指示在关闭和开启,并发送聊天信息过于服务器端