Pārlūkot izejas kodu

Show error message on connect failure

Thomas Dy 11 gadi atpakaļ
vecāks
revīzija
a8e5fab973
2 mainītis faili ar 12 papildinājumiem un 7 dzēšanām
  1. 7 0
      public/javascripts/chatServices.js
  2. 5 7
      public/partials/chatRoom.html

+ 7 - 0
public/javascripts/chatServices.js

@@ -26,10 +26,14 @@ angular.module('chatServices', [])
   };
 
   service.connect = function(username) {
+    service.error = null;
     chatSocket = new WS(jsRoutes.controllers.Application.chat(username).webSocketURL());
     chatSocket.onmessage = wrap(function(event) {
       var message = JSON.parse(event.data);
       console.log(message);
+      if(message.error) {
+        service.error = message.error;
+      }
       if(message.kind != "pong") {
         $rootScope.$broadcast('ws:message', message);
       }
@@ -73,6 +77,9 @@ angular.module('chatServices', [])
       }
       service.messages.push(message);
     },
+    getError: function() {
+      return Connection.error;
+    },
     connect: Connection.connect,
     disconnect: Connection.disconnect,
     isConnected: Connection.isConnected,

+ 5 - 7
public/partials/chatRoom.html

@@ -3,6 +3,11 @@
   <div class="page-header">
     <h1>Not connected</h1>
   </div>
+  <div ng-show="service.getError()" class="alert-message error">
+    <p>
+    <strong>Oops!</strong> <span>{{service.getError()}}</span>
+    </p>
+  </div>
   <div class="span10">
     <p>Please sign in at the top bar.</p>
   </div>
@@ -11,13 +16,6 @@
   <div class="page-header">
     <h1>Welcome <small>You are playing as {{service.username}}</small></h1>
   </div>
-
-  <div id="onError" class="alert-message error">
-    <p>
-    <strong>Oops!</strong> <span></span>
-    </p>
-  </div>
-
   <div class="row">
     <div class="span10" id="main">
       <div id="messages">