2016-12-26 29 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位指示在關閉和開啓,併發送聊天信息過於服務器端