Spring Boot-WebSocket握手期间出错

时间:2019-02-07 17:15:46

标签: spring-boot websocket spring-websocket

当我尝试在春季启动应用程序中使用WebSocket时,出现以下错误:“与'ws:// localhost:8080 / Moda / chatroomServerEndpoint'的WebSocket连接失败:WebSocket握手期间出错:意外的响应代码:400”(默认值.html-Line 11)..我的目标:通过两个不同的选项卡发送消息(例如:在Google Chrome中)谢谢...

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 个答案:

答案 0 :(得分:0)

尝试

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

代替

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

还要确保设置.setAllowedOrigins("*")