谁能提供一个能用的JAVA的Websocket聊天室

在下载频道下载了N个都不能用,项目能跑起来却提示连接不上,在此求各位dalao给一个能用的JAVA的Websocket聊天室,或者指导下这个项目为啥能跑起来却连接不上http://download.csdn.net/download/wendysunday123/9788097

在线等,挺急的,今天解答的后面追加悬赏50

8个回答

---------------------------------------------------------------------------java :DemoServletTwo
package websocket;

import java.io.IOException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import java.util.concurrent.CopyOnWriteArraySet;

import java.util.concurrent.atomic.AtomicInteger;

import javax.websocket.MessageHandler;
import javax.websocket.OnClose;

import javax.websocket.OnError;

import javax.websocket.OnMessage;

import javax.websocket.OnOpen;

import javax.websocket.Session;

import javax.websocket.server.ServerEndpoint;

import entity.Member;

@ServerEndpoint(value = "/websocket/chat")

public class DemoServletTwo {

//private static final Log log = LogFactory.getLog(ChatAnnotation.class);

private static final String GUEST_PREFIX = "Guest";

private static final AtomicInteger connectionIds = new AtomicInteger(0);

private static final Set connections = new CopyOnWriteArraySet();

private final Member member;

private final String nickname;

private Session session;

public DemoServletTwo() {

member = new Member();
nickname = GUEST_PREFIX + connectionIds.getAndIncrement();

}

//建立连接

@OnOpen

public void start(Session session) {

this.session = session;

connections.add(this);
Map param = session.getPathParameters();
if(param.get("name") != null){
member.setName(param.get("name"));
}
if(param.get("sex") != null){
member.setSex(param.get("sex"));
}

  String message = String.format("* %s %s", nickname, "has joined.");  
  System.out.println(message);  

}

//接受消息

@OnMessage

public void incoming(String message) {

System.out.println(message.toString());

//broadcast(filteredMessage);

broadcast(member.talk(message.toString()));

}

//客户端关闭了连接

@OnClose

public void end() {

connections.remove(this);

String message = String.format("* %s %s", nickname, "has disconnected.");

System.out.println(message);

//broadcast(message);

}

//WebSocket服务出错

@OnError

public void onError(Throwable t) throws Throwable {

//log.error("Chat Error: " + t.toString(), t);

}

private static void broadcast(String msg) {

for (DemoServletTwo client : connections) {

try {

synchronized (client) {

client.session.getBasicRemote().sendText(msg);

}

} catch (IOException e) {

//log.debug("Chat Error: Failed to send message to client", e);

connections.remove(client);

try {

client.session.close();

} catch (IOException e1) {

// Ignore

}

String message = String.format("* %s %s",

client.nickname, "has been disconnected.");

broadcast(message);

}

}

}

}

--------------------------------------------------------------------------------------------config: web.xml
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
Archetype Created Web Application

wsServlet
websocket.DemoServlet


wsServlet
/wsServlet


------------------------------------------------------------------------------chat.html
<!DOCTYPE html>



Tomcat WebSocket Chat var ws = new WebSocket("ws://localhost:8080/websocket//websocket/chat"); ws.onopen = function(){ }; ws.onmessage = function(message){ document.getElementById("chatlog").textContent += message.data + "\n"; }; function postToServer(){ ws.send(document.getElementById("msg").value); document.getElementById("msg").value = ""; } function closeConnect(){ ws.close(); }






Send!
End

创建个项目名为:websocket
把这三个文件弄好就能用了

qq_27718453
庄粟 币我收到了,另外你说的不能用是指什么,websocket要比较新的浏览器,我写的这个demo在我同事那打开也跑不了,他的浏览器没更新,希望对你有帮助
大约 3 年之前 回复
prayeri137
prayeri137 回复庄粟: 诶,这个怎么给币的呀?怎么没自动扣呢?第一次用csdn提问
大约 3 年之前 回复
prayeri137
prayeri137 你这个不能用,不过感谢你的勤快
大约 3 年之前 回复
qq_27718453
庄粟 标签没掉了。。。
大约 3 年之前 回复
playboyanta123
show_code 回复prayeri137: ws://是websocket特有的协议,就跟http://一样,你这报错是后台响应出错啊,我没看到你后台的代码不好说,你再针对这个报错去解决一下应该就ok了
大约 3 年之前 回复
prayeri137
prayeri137 搞了一个中午,原网址的例子跑起来了,整合到自己项目就报这个错误,想问下'ws://'格式是什么?'ws://本机地址:端口号/项目名/?'
大约 3 年之前 回复
prayeri137
prayeri137 WebSocket connection to 'ws://127.0.0.1:8080/message' failed: Error during WebSocket handshake: Unexpected response code: 404
大约 3 年之前 回复

连接不上数据库,那你有这个数据库吗?

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>wsServlet</servlet-name>
<servlet-class>websocket.DemoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>wsServlet</servlet-name>
<url-pattern>/wsServlet</url-pattern>
</servlet-mapping>
</web-app>
<!DOCTYPE html>
<html>
<head>
<meta charset=UTF-8>
<title>Tomcat WebSocket Chat</title>
<script>
var ws = new WebSocket("ws://localhost:8080/websocket//websocket/chat");
ws.onopen = function(){
};
ws.onmessage = function(message){
document.getElementById("chatlog").textContent += message.data + "\n";
};
function postToServer(){
ws.send(document.getElementById("msg").value);
document.getElementById("msg").value = "";
}
function closeConnect(){
ws.close();
}
</script>
</head>
<body>
<textarea id="chatlog" readonly></textarea><br/>
<input id="msg" type="text" />
<button type="submit" id="sendButton" onClick="postToServer()">Send!</button>
<button type="submit" id="sendButton" onClick="closeConnect()">End</button>
</body>
</html>

不好意思啊有点乱

图片说明
你这个项目我已经调试好了,这是运行结果,如果需要我发给你,不过你得按照我的配置去做才能成功

hgq0916
奔腾的野马 回复hgq0916: 'ws://127.0.0.1:8080/message是连接的URL地址,ws是websocket协议,后面和一般的项目路径是一样的,message是servlet的URL-pattern,如果有项目名,再加上项目,比如:'ws://127.0.0.1:8080/webSocket/message
大约 3 年之前 回复
hgq0916
奔腾的野马 我的项目环境是jdk1.7,tomcat7.0,eclipse mars2,要在myeclipse上运行需要修改些东西
大约 3 年之前 回复

大佬求源码 邮箱jzeran@163.com

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐