首页 > 解决方案 > JSF websocket url 在 ssl 端口上不起作用

问题描述

我正在使用它创建一个 jsf 聊天应用程序,当我在我的 jsf 应用程序中使用 http它工作正常,但当我在我的 jsf 应用程序中使用 https 时失败。我已经按照这个创建了一个 websocket,它在运行在端口 8080 上的 tomcat 上运行良好,但是当我在 tomcat ssl 端口 8443 上运行我的 jsf 应用程序时,我收到了这个错误。 在此处输入图像描述

这是我正在使用的java代码

import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import javax.websocket.OnClose;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
@ServerEndpoint("/push")
public class PushNofity {

    private static final Set<Session> SESSIONS = ConcurrentHashMap.newKeySet();

    @OnOpen
    public void onOpen(Session session) {
        SESSIONS.add(session);
    }

    @OnClose
    public void onClose(Session session) {
        SESSIONS.remove(session);
    }

    public static void sendAll(String text) {
        System.out.println("on session:" + text);
        synchronized (SESSIONS) {
            for (Session session : SESSIONS) {
                if (session.isOpen()) {

                    session.getAsyncRemote().sendText(text);

                }
            }
        }
    }
}


    @ManagedBean(name = "ch")
    @SessionScoped
    @Getter
    @Setter
    
    public class ChatManagedBean {
    
        StompClient stompSocket;
        
        @PostConstruct
        public void inIt()
        {
          stompSocket = new StompClient(URI.create("ws://127.0.0.1:8080/SpringBootWebsocktAPI/ws"));
          
              try {
                    //stompSocket.setSocketFactory(getSslFactory());
                    connected = stompSocket.connectBlocking();
    
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    return;
                }
    
                if (!connected) {
                    System.out.println("Failed to connect to the socket");
                    return;
                }
                            stompSocket.subscribe("/user/" + USER_ID + "/queue/messages", new StompMessageListener() {
    
                    @Override
                    public void onMessage(StompFrame stompFrame) {
                    
                       ChatMessage chatMessage = gson.fromJson(stompFrame.getBody().trim(), ChatMessage.class);
                    PushNofity.sendAll(chatMessage.getMessage());//this works fine when my jsf url is running on port 8080
                    }
                    });
        
        }
    
    }

和我的 xhtml 代码

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
  
      xmlns:p="http://primefaces.org/ui"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">
    <h:head>

        <title>JSF Chat</title>
        <h:outputStylesheet library="css" name="style.css"/>
       
        <script type="text/javascript">

            if (window.WebSocket) {
                var ws = new WebSocket("wss://localhost:8443/JsChatApp/push");//does not work when my jsf url is running on port 8443
                

                    //var ws = new WebSocket("ws://localhost:8080/JsChatApp/push"); this work fine
             
            }


                
                ws.onmessage = function () {
                    console.log("helo");
   

                    updateChat();

                };
//            }

            $("#chat_panel").animate({scrollTop: 500}, 1000);

            function autoScroll() {


                chatWindow = document.getElementById('chat_panel');
                var xH = chatWindow.scrollHeight;
                chatWindow.scrollTo(0, xH);


            }

        

          

        </script>

        <style>
            *{
                margin: 0px;
                padding: 0px;
            }
        </style>

    </h:head>
    <h:body>
     <h:form>
                <p:remoteCommand oncomplete="autoScroll();" name="updateChat" process="@this"  update="chat_panel" />
            </h:form>

                    <p:outputPanel id="chat_panel" styleClass="chat-container" style="height: 440px;overflow-y: scroll;overflow-x: hidden;margin-bottom: 10px;" >


                        <ui:repeat value="#{ch.messages}" var="t" id="tbl" class="chat_div" varStatus="myVarStatus">

                            <div class="row no-guttersp" style="margin-left: 0px;">

                                <div class="#{ch.alternateChat(t.senderId)}">
                                    #{t.message}
                                    <br></br>
                                    <span>
                                        #{t.chatDate}
                                    </span>
                                </div>

                            </div>



                        </ui:repeat>
                    </p:outputPanel>
    </h:body>
    </html>

我在用;

  1. 素面 8.0
  2. JSF 2.2
  3. 雄猫 8.0.24

标签: ssljsfprimefaces

解决方案


推荐阅读