I am creating a jsf chat application using this which works fine when am using http in my jsf application but fails when I use https in my jsf application. I have followed this to create a websocket and it works fine on tomcat running on port 8080 but when I run my jsf application on tomcat ssl port 8443 am getting this error.
Here is my java code that am using
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
}
});
}
}
and my xhtml code
<?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>
I am using;
- primefaces 8.0
- JSF 2.2
- Tomcat 8.0.24