Skip to content

Commit

Permalink
init repository
Browse files Browse the repository at this point in the history
  • Loading branch information
HWZen committed Apr 19, 2022
0 parents commit 558f0ce
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea
lib
out
target
19 changes: 19 additions & 0 deletions EChat.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="FacetManager">
<facet type="web" name="Web">
<configuration>
<descriptors>
<deploymentDescriptor name="web.xml" url="file://$MODULE_DIR$/web/WEB-INF/web.xml" />
</descriptors>
<webroots>
<root url="file://$MODULE_DIR$/web" relative="/" />
</webroots>
<sourceRoots>
<root url="file://$MODULE_DIR$/src/main/java" />
<root url="file://$MODULE_DIR$/src/main/resources" />
</sourceRoots>
</configuration>
</facet>
</component>
</module>
30 changes: 30 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<dependencies>
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>1.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
</dependency>
</dependencies>

<groupId>org.example</groupId>
<artifactId>EChat</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

</project>
40 changes: 40 additions & 0 deletions src/main/java/serverTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import java.io.IOException;
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(value="/websocketTest/{userId}")
public class serverTest {
private static String userId;

// new Connection
@OnOpen
public void onOpen(Session session, @PathParam("userId") String userId) {
this.userId = userId;
System.out.println("Connected to user " + userId);
}

// close Connection
@OnClose
public void onClose(Session session) {
System.out.println("Disconnected from user " + userId);
}

// receive message
@OnMessage
public void onMessage(String message, Session session) {
System.out.println("Received message: " + message);
}

// error
@OnError
public void onError(Session session, Throwable error) {
System.out.println("User: " + userId);
System.out.println("Error: " + error.getMessage());
}

}
11 changes: 11 additions & 0 deletions web/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

86 changes: 86 additions & 0 deletions web/index.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<%--
Created by IntelliJ IDEA.
User: HWZ
Date: 2022/4/19
Time: 15:53
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
websocket Demo---- user000 <br />
<input id="userName" type="text" />
<button onclick="connect2server()"> Connect </button>
<br />

<input id="text" type="text" />
<button onclick="send()"> Send </button>
<button onclick="closeWebSocket()"> Close </button>
<div id="message"> </div>

<script type="text/javascript">
let websocket;
function connect2server(){
//判断当前浏览器是否支持WebSocket
if('WebSocket' in window){
var userName = document.getElementById("userName").value;
websocket = new WebSocket("ws://localhost:8080/EChat_Web_exploded/websocketTest/"+userName);
setMessageInnerHTML("Connect success");
console.log("link success")
}else{
setMessageInnerHTML("当前浏览器不支持websocket");
alert('Not support websocket')
}
}
//连接发生错误的回调方法
websocket.onerror = function(){
setMessageInnerHTML("error");
};
//连接成功建立的回调方法
websocket.onopen = function(event){
setMessageInnerHTML("open");
}
console.log("-----")
//接收到消息的回调方法
websocket.onmessage = function(event){
setMessageInnerHTML(event.data);
}
//连接关闭的回调方法
websocket.onclose = function(){
setMessageInnerHTML("close");
}
//监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。
window.onbeforeunload = function(){
websocket.close();
}
//将消息显示在网页上
function setMessageInnerHTML(innerHTML){
document.getElementById('message').innerHTML += innerHTML + '<br/>';
}
//关闭连接
function closeWebSocket(){
websocket.close();
}
//发送消息
function send(){
var message = document.getElementById('text').value;
websocket.send(message);
}
</script>

</body>
</html>

0 comments on commit 558f0ce

Please sign in to comment.