基于websocket的多人实时聊天效果问题

qq_28815359 2015-06-25 08:14:16
这是html页面

<!DOCTYPE html>
<html>
<head>
<meta name="author" content="Yeeku.H.Lee(CrazyIt.org)" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title> 使用WebSocket通信 </title>
<script type="text/javascript">
// 创建WebSocket对象
var webSocket = new WebSocket("ws://127.0.0.1:8888/WebSocket/chat");
var sendMsg = function()
{
var inputElement = document.getElementById('msg');
// 发送消息
webSocket.send(inputElement.value);
// 清空单行文本框
inputElement.value = "";
}
var send = function(event)
{
if (event.keyCode == 13)
{
sendMsg();
}
};
webSocket.onopen = function()
{
// 为onmessage事件绑定监听器,接收消息
webSocket.onmessage= function(event)
{
var show = document.getElementById('show')
// 接收、并显示消息
show.innerHTML += event.data + "<br/>";
show.scrollTop = show.scrollHeight;
}
document.getElementById('msg').onkeydown = send;
document.getElementById('sendBn').onclick = sendMsg;
};
webSocket.onclose = function ()
{
document.getElementById('msg').onkeydown = null;
document.getElementById('sendBn').onclick = null;
Console.log('WebSocket已经被关闭。');
};
</script>
</head>
<body>
<div style="width:600px;height:240px;
overflow-y:auto;border:1px solid #333;" id="show"></div>
<input type="text" size="80" id="msg" name="msg" placeholder="输入聊天内容"/>
<input type="button" value="发送" id="sendBn" name="sendBn"/>
</body>
</html>

这是服务器程序

import java.io.IOException;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicInteger;

import javax.websocket.*;
import javax.websocket.server.*;

@ServerEndpoint("/2.14test/chat")
public class ChatEntpoint {
private static final String GUEST_PREFIX = "访客";
private static final AtomicInteger connectionIds = new AtomicInteger();
private static final Set<ChatEntpoint> clientset = new CopyOnWriteArraySet<>();
private final String nickname;
private Session session;

public ChatEntpoint() {
nickname = GUEST_PREFIX + connectionIds.getAndIncrement();
}

// 当客户端连接进来时自动激发该方法
@OnOpen
public void start(Session session) {
this.session = session;
// 将WebSocket客户端会话添加到集合中
clientset.add(this);
String message = String.format("[%s %s]", nickname, "加入了聊天室");
// 发送消息
broadcast(message);
}

@OnClose
public void end() {
clientset.remove(this);
String message = String.format("[%s %s]", nickname, "離開了聊天室");
broadcast(message);
}

@OnMessage
public void incoming(String message) {
String filteredMessage = String.format("%s:%s", nickname,
filter(message));
// 发送消息
broadcast(filteredMessage);
}

@OnError
public void onError(Throwable t) throws Throwable {
System.out.println("WebSocket服务断错误");
}

private static void broadcast(String msg) {
for (ChatEntpoint client : clientset) {
try{
synchronized(client){
//发送消息
client.session.getBasicRemote().sendText(msg);
}

}
catch(Exception e){
System.out.print("聊天错误,像客户端"+client+"发送消息出现错误。");
clientset.remove(client);
try{
client.session.close();
}
catch(Exception e1){
e1.printStackTrace();
}
String message=String.format("[%s %s]", client.nickname,"已断开了连接");


}
}
}
private static String filter(String message){
if(message==null)
return null ;
char content[]=new char[message.length()];
message.getChars(0, message.length(), content,0);
StringBuilder result=new StringBuilder(content.length+50);
for(int i=0;i<content.length;i++)
{
//控制对尖括号等特殊字符进行转义
switch(content[i]){
case '<':
result.append("<");
break;
case '>':
result.append(">");
break;
case '&':
result.append("&");
break;
case '"':
result.append(""");
break;
default:
result.append(content[i]);
}

}
return (result.toString());

}
}

两个代码都没提示错误但是为什么功能不能正常运行的?求大神帮忙
...全文
1346 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaoyan_2018 2019-04-10
  • 打赏
  • 举报
回复
vue2.0+webpack+vuex实现的仿微信聊天室|仿微信聊天界面|仿微信模板
https://blog.csdn.net/yanxinyun1990/article/details/89038427
mengke.me 2019-01-31
  • 打赏
  • 举报
回复
ws://127.0.0.1:8888/WebSocket/chat 改成 ws://127.0.0.1:8888/WebSocket/2.14test/chat

或者

@ServerEndpoint("/2.14test/chat") 改成 @ServerEndpoint("/chat")

andy__YxY 2018-06-14
  • 打赏
  • 举报
回复
前些时间开发的一个项目,h5高仿微信聊天案例,挺不错的,大家可以去看下。 https://blog.csdn.net/xiaoyan_2015/article/details/80189361
qq_26849915 2017-02-23
  • 打赏
  • 举报
回复
看看是不是在哪里配置了拦截器什么的 去掉
DaveLeeCN 2015-06-30
  • 打赏
  • 举报
回复
可能还是哪一块有问题,楼主看看报什么错,然后查看报错的位置
DaveLeeCN 2015-06-30
  • 打赏
  • 举报
回复
谢谢楼主分享,第一次报找不到chat路径,按1楼改完之后搞定,代码已收藏!
qq_28815359 2015-06-26
  • 打赏
  • 举报
回复
还是不行
引用 1 楼 qq542369628 的回复:
@ServerEndpoint("/2.14test/chat")
改成
@ServerEndpoint("/chat")
试试
Waitforsniping 2015-06-26
  • 打赏
  • 举报
回复
@ServerEndpoint("/2.14test/chat")
改成
@ServerEndpoint("/chat")
试试
在这个互联网时代,客服可以说必不可少,每个电商网站都应该有一个强大的智能客服对话系统,以满足用户沟通的需求。智能客服对话系统,不仅需要人工的沟通,同时结合人工智能实现智能对话,减少人工客服的成本,势在必行。基于SpringBoot+Python的多语言前后端智能多人聊天系统课程,将以基础知识为根基,带大家完成一个强大的智能客服系统,该系统将包含以下功能:智能对话机器人、单聊、群聊、消息撤回、上线、下线通知、用户动态信息实时提示等。即时通讯和人工智能,在未来的发展趋势,必然需要大批人才,掌握这两个技术势在必行。项目是一个真实可用的项目,商业价值不言而喻。也可以基于课程的基础上进一步完善和优化,所以价值是很高的。本课程包含的技术: 开发工具为:IDEA、WebStorm、PyCharmTensorflowRNNLSTMAnacondaSpringBoot SpringCloudWebsocketSTOMPDjangoVue+Nodejs+jQuery等 课程亮点: 1.与企业接轨、真实工业界产品2.从基础到案例,逐层深入,学完即用3.市场主流的前后端分离架构和人工智能应用结合开发4.多语言结合开发,满足多元化的需求5.涵盖TensorFlow1.x+TensorFlow2.x版本6.智能机器人实战7.即时通讯实战8.多Python环境切换9.微服务SpringBoot10.集成SpringCloud实现统一整合方案 11.全程代码实操,提供全部代码和资料 12.提供答疑和提供企业技术方案咨询 课程目录:第一章、Anaconda以及TensorFlow环境和使用0、智能多人聊天系统课程说明1、智能多人聊天系统之Anaconda讲解2、智能多人聊天系统之Anaconda安装和使用3、智能多人聊天系统之Anaconda之conda命令使用4、智能多人聊天系统之TensorFlow讲解5、智能多人聊天系统之TensorFlow安装和使用6、TensorFlow常量、变量和占位符实战讲解17、TensorFlow常量、变量和占位符实战讲解28、TensorFlow原理补充讲解9、TensorFlow四则运算实战讲10、TensorFlow矩阵操作以及运算实战讲解111、TensorFlow矩阵操作以及运算实战讲解212、TensorFlow均匀分布和正态分布数据实战讲解13、智能多人聊天系统之Numpy实战讲解14、智能多人聊天系统之matplotlib实战讲解15、TensorFlow深度学习DNN讲解16、TensorFlow常用Python扩展包讲解17、TensorFlow常用回归算法以及正则化讲解18、TensorFlow损失函数定义和使用实战讲解19、TensorFlow优化器讲解以及综合案例实战讲解20、智能多人聊天系统之RNN讲解21、智能多人聊天系统之RNN种类讲解22、智能多人聊天系统之RNN代码实战23、智能多人聊天系统之LSTM讲解24、智能多人聊天系统之attention机制讲解25、智能多人聊天系统之Django环境构建及初体验26、智能多人聊天系统之Django开发27、Python章节环境侯建和项目搭建28、Python TensorFlow读取训练数据代码编写29、Python TensorFlow形成语料编码30、Python TensorFlow保存字典文件31、Python TensorFlow构建词向量32、Python TensorFlow构建lstm模型以及attention wrapper33、Python TensorFlow训练代码编写34、Python整体代码讲解35、Python运用模型代码讲解36、SpringBoot讲解以及构建web应用37、Spring Cloud注册中心构建38、智能多人聊天系统之前端Vue项目构建39、SpringBoot+Websocket群聊40、SpringBoot+Websocket昵称群聊41、SpringBoot+Websocket群聊+单聊实战42、SpringBoot+Stomp单聊143、SpringBoot+Stomp单聊244、SpringBoot+Stomp单聊+群聊45、Django Web整合TF代码讲解及Postman调试46、智能客服系统单聊群聊等项目功能代码讲解147、智能客服系统单聊群聊等项目功能代码讲解248、智能客服系统集成机器人对话代码开发讲解49、智能机器人TensorFlow2版本升级实战之训练模型代码讲解50、智能机器人TensorFlow2版本升级实战之预测代码讲解 51、智能机器人TensorFlow2版本升级实战补充讲解

81,091

社区成员

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

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