通过websocket客户端向服务器发消息时,报400错误

baidu_27100831 2015-04-03 02:24:26
客户端代码
var webSocket = new WebSocket(
'ws://127.0.0.1:8080/websocket');

webSocket.onerror = function(event) {
onError(event)
};

webSocket.onopen = function(event) {
onOpen(event)
};

webSocket.onmessage = function(event) {
onMessage(event)
};

function onMessage(event) {
document.getElementById('messages').innerHTML += '<br />'
+ event.data;
}

function onOpen(event) {
document.getElementById('messages').innerHTML = 'Connection established';
}

function onError(event) {
alert(event.data);
}

function start2() {
webSocket.send('hello');
return false;
}


服务端代码:
import java.io.IOException;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint(value="/websocket")
public class WebSocketTest {

@OnMessage
public void onMessage(String message, Session session)
throws IOException, InterruptedException {

// Print the client message for testing purposes
System.out.println("Received: " + message);

// Send the first message to the client
session.getBasicRemote().sendText("This is the first server message");

// Send 3 messages to the client every 5 seconds
int sentMessages = 0;
while(sentMessages < 3){
Thread.sleep(5000);
session.getBasicRemote().
sendText("This is an intermediate server message. Count: "
+ sentMessages);
sentMessages++;
}

// Send a final message to the client
session.getBasicRemote().sendText("This is the last server message");
}

@OnOpen
public void onOpen () {
System.out.println("Client connected");
}

@OnClose
public void onClose () {
System.out.println("Connection closed");
}
}


启动后报这个错误:
WebSocket connection to 'ws://127.0.0.1:8080/websocket' failed: Error during WebSocket handshake: Unexpected response code: 400
求解!!!!!!!!!!!!!!!!!!!!!!!!!!!
...全文
4661 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
baidu_27100831 2015-04-03
  • 打赏
  • 举报
回复
直接下载的demo,在本地跑起来也是会报400错误,本地环境是java8.0,tomcat8.0,配置没什么问题,不知道是不是jar包冲突,
<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
<dependency>  
    <groupId>org.springframework</groupId>  
    <artifactId>spring-webmvc</artifactId>  
    <version>3.2.8.RELEASE</version>  
</dependency>
  	<dependency>
  		<groupId>org.hibernate</groupId>
  		<artifactId>hibernate-commons-annotations</artifactId>
  		<version>3.2.0.Final</version>
  	</dependency>
  	<dependency>
  		<groupId>org.hibernate</groupId>
  		<artifactId>hibernate-core</artifactId>
  		<version>4.2.0.Final</version>
  	</dependency>
  	<dependency>
  		<groupId>org.hibernate.javax.persistence</groupId>
  		<artifactId>hibernate-jpa-2.0-api</artifactId>
  		<version>1.0.1.Final</version>
  	</dependency>
  <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc14</artifactId>
            <version>10.2.0.1.0</version>
        </dependency>
  <dependency>
  	<groupId>org.springframework</groupId>
  	<artifactId>spring-orm</artifactId>
  	<version>3.1.2.RELEASE</version>
  </dependency>
  <dependency>
  	<groupId>org.hibernate</groupId>
  	<artifactId>hibernate-ehcache</artifactId>
  	<version>4.2.0.Final</version>
  </dependency>
  <dependency>
  	<groupId>org.slf4j</groupId>
  	<artifactId>slf4j-api</artifactId>
  	<version>1.7.2</version>
  </dependency>
  <dependency>
  	<groupId>org.hibernate</groupId>
  	<artifactId>hibernate-validator</artifactId>
  	<version>4.3.1.Final</version>
  </dependency>
 
  </dependencies>
  <build>
    <finalName>ChineseChess</finalName>
    <plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>
		</plugins>
  </build>
</project>
大哥给我看看,困扰很久了
lyw985 2015-04-03
  • 打赏
  • 举报
回复
wobsocket没人用额,当然没人帮你了 我只能提一些参考性建议 1.学习部署demo时不要修改内容,例如start2() 2.检查相关配置是否遗漏 3.查看错误信息提示以及处理方式,比如这个400错误,指的是语法错误,应该是“ws://127.0.0.1:8080/websocket”无法被解析
baidu_27100831 2015-04-03
  • 打赏
  • 举报
回复
没人么???

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧