1 Reply Latest reply on Sep 19, 2014 11:29 PM by aupres

    My java-script websocket client does not work.

    aupres

      This is my simple websocket project with eclipse kepler. The project is dynamic web project named "WSEchoAllWeb"

      And these are my codes. First, websocket endpoint class

       

      @ServerEndpoint("/echo")

      public class EchoAllEndpoint {

       

      @OnMessage

      public void onMessage(Session session, String msg) {

        try {

         for (Session sess : session.getOpenSessions()) {

          if (sess.isOpen())

           sess.getBasicRemote().sendText(msg);

         }

        } catch (IOException e) {

         e.printStackTrace();

        }

      }

      }

       

      Deployment is ok. The deployment success logs are diplayed on console like below

      14:56:05,821 INFO  [io.undertow.websockets.jsr] (MSC service thread 1-6) UT026003: Adding annotated server endpoint class com.aaa.ws.EchoAllEndpoint for path /echo

      14:56:05,825 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-6) JBAS017534: Registered web context: /WSEchoAllWeb

      14:56:05,849 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 1) JBAS018559: Deployed "WSEchoAllWeb.war" (runtime-name : "WSEchoAllWeb.war"

       

      But I think the problem is client html file containing java script, below is my client html file

       

      <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
      <title>WebSocket client #1</title>
      </head>
      <script language="javascript" type="text/javascript">
      var wsUri = "ws://localhost:8080/WSEchoAllWeb/echo";

      var websocket = new WebSocket(wsUri); // websocket object is not created at all!

        
          websocket.onopen = function(evt) { onOpen(evt) };
          websocket.onmessage = function(evt) { onMessage(evt) };
          websocket.onerror = function(evt) { onError(evt) };

          function send_msg() {
        alert(textID.value);
           alert(websocket);
           websocket.send("[client #1] " + textID.value);
           
             writeToScreen("SENT from client #1: " + textID.value);
          }

          function onOpen(evt) {
             writeToScreen("CONNECTED");
          }

          function onMessage(evt) {
             writeToScreen("RECEIVED: " + evt.data);
          }

          function onError(evt) {
             writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
          }

          function writeToScreen(message) {
             var pre = document.createElement("p");
             pre.style.wordWrap = "break-word";
             pre.innerHTML = message;
             output.appendChild(pre);
          }
      </script>
      <body>
      <input id="textID" name="message" type="text">
      <input onclick="send_msg()" value="전송" type="button"><p>
      <div id="output"></div>
      </body>
      </html>

       

      This client script code can not create websocket connection!

      Any advice will be deeply appreciated! Thanks