Java实现网页一对一 一对多实时聊天websocket
后台代码 最重要的类
package com
.socket
.websocket
.chat
;
import com
.alibaba
.fastjson
.JSON;
import com
.alibaba
.fastjson
.JSONArray
;
import com
.alibaba
.fastjson
.JSONObject
;
import org
.springframework
.stereotype
.Component
;
import java
.io
.File
;
import java
.io
.IOException
;
import java
.util
.Hashtable
;
import java
.util
.Map
;
import javax
.websocket
.OnClose
;
import javax
.websocket
.OnError
;
import javax
.websocket
.OnMessage
;
import javax
.websocket
.OnOpen
;
import javax
.websocket
.Session
;
import javax
.websocket
.server
.PathParam
;
import javax
.websocket
.server
.ServerEndpoint
;
@
ServerEndpoint("/webSocket/{senderId}")
@Component
public class WebChat {
private static Map
<String
, WebChat
> webSocketMap
= new Hashtable();
private Session session
;
@OnOpen
public void onOpen(@
PathParam(value
="senderId") String senderId
, Session session
) {
this.session
= session
;
webSocketMap
.put(senderId
,this);
System
.out
.println(senderId
+"连接加入!当前在线人数为"+getOnlineCount());
}
@OnClose
public void onClose(@
PathParam(value
="senderId") String userId
) {
webSocketMap
.remove(userId
);
System
.out
.println(userId
+"关闭连接!当前在线人数为" + getOnlineCount());
}
@OnMessage
public void onMessage(@
PathParam(value
="senderId") String senderId
, String message
, Session session
) {
try {
JSONObject jsonMessge
= JSON.parseObject(message
);
JSONArray list
= jsonMessge
.getJSONArray("userList");
WebChat userMap
= webSocketMap
.get(senderId
);
String mess
=jsonMessge
.getString("mess");
userMap
.sendMessage(mess
);
for (Object receiverId
: list
) {
WebChat receiver
= webSocketMap
.get(receiverId
);
receiver
.sendMessage(mess
);
}
} catch (Exception e
) {
e
.printStackTrace();
}
}
@OnError
public void onError(@
PathParam(value
="userId") String userId
, Session session
, Throwable error
) {
System
.out
.println(userId
+"发生错误");
error
.printStackTrace();
}
public void sendMessage(String message
) throws IOException
{
this.session
.getBasicRemote().sendText(message
);
}
public void sendFile(File file
)throws IOException
{
this.session
.getAsyncRemote().sendObject(file
);
}
public static synchronized int
getOnlineCount() {
return webSocketMap
.size();
}
}
配置类
package com
.socket
.websocket
.config
;
import org
.springframework
.context
.annotation
.Bean
;
import org
.springframework
.context
.annotation
.Configuration
;
import org
.springframework
.web
.socket
.server
.standard
.ServerEndpointExporter
;
@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter
serverEndpointExporter() {
System
.out
.println(">>>>>>>>>>>>>>>>>启用 WebSocket");
return new ServerEndpointExporter();
}
}
pom.xml
<!--websocket连接需要使用到的包
-->
<dependency
>
<groupId
>javax
.websocket
</groupId
>
<artifactId
>javax
.websocket
-api
</artifactId
>
<version
>1.1</version
>
<scope
>provided
</scope
>
</dependency
>
<dependency
>
<groupId
>org
.springframework
.boot
</groupId
>
<artifactId
>spring
-boot
-starter
-websocket
</artifactId
>
</dependency
>
<dependency
>
<groupId
>javax
</groupId
>
<artifactId
>javaee
-api
</artifactId
>
<version
>7.0</version
>
<scope
>provided
</scope
>
</dependency
>
<!-- webSocket 结束
-->
<!-- fastjson
JSON包
-->
<dependency
>
<groupId
>com
.alibaba
</groupId
>
<artifactId
>fastjson
</artifactId
>
<version
>1.2.31</version
>
</dependency
>
前端代码
<!DOCTYPE html
>
<html lang
="en">
<head
>
<meta charset
="UTF-8">
<title
>Title
</title
>
</head
>
<style type
="text/css">
input#chat
{
width
: 410px
}
#console
-container
{
width
: 400px
;
}
#console
{
border
: 1px solid #
CCCCCC;
border
-right
-color
: #
999999;
border
-bottom
-color
: #
999999;
height
: 170px
;
overflow
-y
: scroll
;
padding
: 5px
;
width
: 100%;
}
#console p
{
padding
: 0;
margin
: 0;
}
</style
>
<body
>
<div
>
<p
>
<input type
="text" placeholder
="type and press enter to chat" id
="chat" />
</p
>
<div id
="console-container">
<div id
="console"></div
>
</div
>
<script type
="text/javascript">
var Console
= {};
var mess
={};
var userList
=[];
var senderId
=prompt("请输入你的账号");
Console
.log
= (function(message
) {
var console
= document
.getElementById('console');
var p
= document
.createElement('p');
p
.style
.wordWrap
= 'break-word';
p
.innerHTML
= message
;
console
.appendChild(p
);
while (console
.childNodes
.length
> 25) {
console
.removeChild(console
.firstChild
);
}
console
.scrollTop
= console
.scrollHeight
;
});
var Chat
= {};
Chat
.socket
= null;
Chat
.connect
= (function(host
) {
if ('WebSocket' in window
) {
Chat
.socket
= new WebSocket(host
);
} else if ('MozWebSocket' in window
) {
Chat
.socket
= new MozWebSocket(host
);
} else {
Console
.log('错误');
return;
}
Chat
.socket
.onopen = function() {
Console
.log('打开连接');
document
.getElementById('chat').onkeydown = function(event
) {
if (event
.keyCode
== 13) {
Chat
.sendMessage();
}
};
};
Chat
.socket
.onclose = function() {
document
.getElementById('chat').onkeydown
= null;
Console
.log('关闭.');
};
Chat
.socket
.onmessage = function(message
) {
Console
.log(message
.data
);
};
});
Chat
.initialize = function() {
Chat
.connect('ws://127.0.0.1:8080/webSocket/'+senderId
);
};
Chat
.sendMessage
= (function() {
var message
= document
.getElementById('chat').value
;
var receiverIds
=prompt("请输入接收者账号用逗号分隔");
userList
=receiverIds
.split(",");
mess
.userList
=userList
;
mess
.mess
=message
;
var strmee
=JSON.stringify(mess
)
if (message
!= '') {
Chat
.socket
.send(strmee
);
document
.getElementById('chat').value
='';
}
});
Chat
.initialize();
</script
>
</div
>
</body
>
</html
>
第一次发帖…复制别人的稍微做了修改 可以把html打开多个页面进行测试 用的是spring boot 有条件的可以在 我的主页去找源码 没条件的去这里 https://pan.baidu.com/s/1DcnrdiSlh1CQAe9SUg5rcg 取件码:hhva
转载请注明原文地址: https://yun.8miu.com/read-55098.html