前言:
今天各位老铁们对“java中websocket”可能比较着重,各位老铁们都想要知道一些“java中websocket”的相关知识。那么小编也在网摘上网罗了一些关于“java中websocket””的相关文章,希望咱们能喜欢,看官们快快来了解一下吧!今天来给大家介绍一下java中怎么使用Websocket
首先我们创建客户端页面,在这里我是用的是jsp页面,首先我们要导入javaee-api.7.0jar,tomact使用7.0以上版本。好了,直接上页面,创建一个jsp页面,
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<input id="text" type="text"/>
<button onclick="send()">发送消息</button>
<hr/>
<button onclick="closeWebSocket()">关闭WebSocket连接</button>
<hr/>
<div id="message"></div>
</body>
<script type="text/javascript">
var websocket=new WebSocket("ws://localhost:8080/WebSocket/eoch");
//连接
websocket.onopen=function(){
document.getElementById("message").innerHTML="WebSocket连接成功。。。";
}
//websocket连接发生错误
websocket.onerror=function(){
document.getElementById("message").innerHTML="WebSocket连接异常。。。";
}
//关闭websocket连接
websocket.onclose=function(){
document.getElementById("message").innerHTML="WebSocket关闭成功。。。";
}
//发送消息
websocket.onmessage=function(event){
document.getElementById("message").innerHTML=event.data;
}
//监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。
window.onbeforeunload = function () {
closeWebSocket();
}
function closeWebSocket(){
websocket.close();
}
function send(){
var content=document.getElementById("text").value;
websocket.send(content);//发送信息
}
</script>
</html>
以上就是客户端程序,接下来创建服务器端
package com.common;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArraySet;
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;
@ServerEndpoint(value="/eoch")
public class HttpWebSocket {
private static int onlineCount=0;//在线人数
private static CopyOnWriteArraySet<HttpWebSocket> webSocketSet = new CopyOnWriteArraySet<HttpWebSocket>();
private Session session;
//连接websocket
@OnOpen
public void onopen(Session session){
this.session=session;
webSocketSet.add(this);
addOnlineCount();
System.out.println("有新的连接加入,当前在线人数"+onlineCount);
}
@OnClose
public void onclose(){
webSocketSet.remove(this);
subOnlineCount();
System.out.println("有一连接关闭!当前在线人数为" + getOnlineCount());
}
@OnError
public void onError(Session session,Throwable e){
System.out.println("websocket连接异常");
e.printStackTrace();
}
public void sendMessage(String message) throws IOException{
this.session.getBasicRemote().sendText(message);
}
@OnMessage
public void onMessage(String message, Session session) {
System.out.println("来自客户端的消息:" + message);
//群发消息
for(HttpWebSocket item: webSocketSet){
try {
item.sendMessage(message);
} catch (IOException e) {
e.printStackTrace();
continue;
}
}
}
private int getOnlineCount() {
return onlineCount;
}
private static synchronized void addOnlineCount() {
onlineCount++;
}
private static synchronized void subOnlineCount() {
onlineCount--;
}
}
出现以上界面,表示你的websocket已经连接成功了,有兴趣的朋友欢迎加群咨询
591456302
标签: #java中websocket