2014-02-08 53 views
0

這裏是我的代碼,我得到的JS WS工作,但是當y嘗試將消息發送到的WebSocket:GlassFish的回報:如何在glassfish 4和jsf中使用websocket?

INFO: Error : SessionImpl{uri=/Formosa2/endpoint, id='b821d249-6435-45fe-812d- 577bc5fc8fca', endpoint=EndpointWrapper{endpointClass=null, [email protected], uri='/Formosa2/endpoint', contextPath='/Formosa2'}} 

@ServerEndpoint(value = "/endpoint") 
@Singleton 
public class websocket { 
private static Set<Session> peers = Collections.synchronizedSet(new HashSet<Session>()); 

@OnMessage 
public void onMessage(String message) { 
     String filteredMessage = String.format("%s: %s", message.toString()); 
     broadcast(filteredMessage); 
     System.out.println(message); 
} 

@OnOpen 
public void onOpen(Session peer){ 
    peers.add(peer); 
    String message = String.format("* %s %s", "User has joined."); 
    broadcast(message); 
} 

@OnClose 
public void onClose(Session peer){ 
    peers.remove(peer); 
} 

@OnError 
public void onError(Session aclientSession, Throwable aThrowable) { 
    System.out.println("Error : " + aclientSession); 
} 

private void broadcast(String message) { 
     for (Session peer : peers) { 
      try { 
       CharBuffer buffer = CharBuffer.wrap(message); 
       peer.getBasicRemote().sendText(buffer.toString()); 
      } catch (IOException ignore) { 
       // Ignore 
      } 
     } 
    } 
} 

 <script type="text/javascript"> 

      ws = new WebSocket('ws://' + window.location.host + '/Formosa2/endpoint'); //Annotation 

    ws.onopen = function(event) { 
      Console.log('Info: WebSocket connection opened.'); 
        document.getElementById('chat').onkeydown = function(event) { 
         if (event.keyCode == 13) { 
          sendMessage(); 
         } 
        }; 
    }; 

    ws.onmessage = function(event) { 
       sendMessage(event); 
    }; 

    ws.onclose = function(event) { 
        document.getElementById('chat').onkeydown = null; 
        Console.log('Info: WebSocket closed.'); 
    }; 

    ws.onerror = function(event){ 
     alert("Error : " + event.data); 
        Console.log(message.data); 
    }; 


      function sendMessage (event) { 
       var text = document.getElementById('form:texto').value; 
       var select = document.getElementById('form:empresaidEmpresa'); 
       var text2 = document.getElementById('inicio_input').value; 

       var name = select.options[select.selectedIndex].text; 
        ws.send(texto +', '+ name +',' + text2); 
        document.getElementById('chat').value = ''; 
      } 

      var Console = {}; 

      Console.log = (function(message) { 
       var console = document.getElementById('console'); 
       var p = document.createElement('p'); 
       p.style.wordWrap = 'break-word'; 
       p.innerHTML = message; 
       console.appendChild(p); 
       while (console.childNodes.length > 25) { 
        console.removeChild(console.firstChild); 
       } 
       console.scrollTop = console.scrollHeight; 
      }); 

    //   Chat.initialize(); 

     </script> 

回答

0
String message = String.format("* %s %s", "User has joined."); 

此行是問題 格式修飾符是不足的,我可以通過讀取aThrowable參數的日誌(system.out ..)來發現它。

+0

你是如何解決這個問題的? – mstrap

+0

@OnError public void onError(Session aclientSession,Throwable aThrowable){System.out.println(「Error:」+ aclientSession); System.out.println(「Error:」+ aThrowable); } –

+0

我仍然不明白解決方案 - 如果您能夠改進您的答案並提供更多關於問題的解決方案和解決問題的更多細節,那將是非常棒的。謝謝! – mstrap

相關問題