1

When I tried using WebSocket in my spring boot application I got this error : "WebSocket connection to 'ws://localhost:8080/Moda/chatroomServerEndpoint' failed: Error during WebSocket handshake: Unexpected response code: 400" (Default.html-Line 11).. My aim : Send message by two different tabs (ex: in Google Chrome) Please help me! Thank you...

Default.html

<!<!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Deneme</title> <script type = "text/javascript">	var websocket = new WebSocket("ws://localhost:8080/Moda/chatroomServerEndpoint");	websocket.onmessage = function processMessage(message) {	var jsonData = JSON.parse(message.data);	if(jsonData.message != null) messagesTextArea.value += jsonData.message + "\n";	}	function sendMessage(){	websocket.send(messageText.value);	messageText.value = "";	} </script>	</head> <body> <textarea id="messagesTextArea" readonly = "readonly" rows="10" cols="45"></textarea><br/> <input type = "text" id="messageText" size="50" /><input type="button" value="Send" onclick="sendMessage();" />	</body> </html>

ModaApplication.java

package com.moda; import java.io.IOException; import java.io.StringWriter; import java.util.Collections; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import javax.json.Json; import javax.json.JsonObject; import javax.json.JsonWriter; import javax.websocket.OnClose; import javax.websocket.OnMessage; import javax.websocket.OnOpen; import javax.websocket.Session; import javax.websocket.server.ServerEndpoint; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.web.socket.config.annotation.EnableWebSocket; import com.moda.videochat.MyWebSocketConfigurator; @Configuration @EnableAutoConfiguration @ServerEndpoint("/chatroomServerEndpoint") public class ModaApplication {	public static void main(String[] args) {	SpringApplication.run(ModaApplication.class, args);	}	static Set<Session> chatroomUsers = Collections.synchronizedSet(new HashSet<Session>());	@OnOpen	public void handleOpen(Session userSession)	{	chatroomUsers.add(userSession);	}	@OnMessage	public void handleMessage(String message, Session userSession) throws IOException {	String username = (String) userSession.getUserProperties().get("username");	if(username == null)	{	userSession.getUserProperties().get("username");	userSession.getBasicRemote().sendText(buildJsonData("System","you are now connected as " + message));	}	else	{	Iterator<Session> iterator = chatroomUsers.iterator();	while(iterator.hasNext()) iterator.next().getBasicRemote().sendText(buildJsonData(username,message));	}	}	@OnClose	public void handleClose(Session userSession)	{	chatroomUsers.remove(userSession);	}	private String buildJsonData(String username, String message)	{	JsonObject jsonObject = Json.createObjectBuilder().add("message",username+": "+message).build();	StringWriter stringWriter = new StringWriter();	try (JsonWriter jsonWriter = Json.createWriter(stringWriter)) {jsonWriter.write(jsonObject);}	return null;	} }

1 Answer 1

4

Try

new WebSocket("ws://localhost:8080/Moda/chatroomServerEndpoint/websocket");

instead of

`new WebSocket("ws://localhost:8080/Moda/chatroomServerEndpoint");` 

Also make sure to set .setAllowedOrigins("*")

Sign up to request clarification or add additional context in comments.

4 Comments

how set .setAllowedOrigins("*") ? can you give sample ?
I suppose he was meaning to add that method call inside WebsocketBrokerConfiguration class, inside registerStompEndpoints class method, like this: registry.addEndpoint("/vrp-websocket") .setAllowedOrigins("*") .withSockJS();
The problem is Spring Boot will typically not allow setAllowedOrigins("*"). You will need to be more explicit eg setAllowedOrigins("localhost:4200")
I can't see any logic behind using the url "ws://localhost:8080/Moda/chatroomServerEndpoint/websocket" when the server end point is "chatroomServerEndpoint". Why do you have to add "/websocket"? However, that is the only way I could get this to work. But I couldn't find any documentation requiring "/websocket" to be added to the endpoint.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.