Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a0c7129

Browse files
netty之WebSocket demo
0 parents commit a0c7129

File tree

17 files changed

+1053
-0
lines changed

17 files changed

+1053
-0
lines changed

‎.gitignore

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
HELP.md
2+
target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
!**/src/main/**
5+
!**/src/test/**
6+
7+
### STS ###
8+
.apt_generated
9+
.classpath
10+
.factorypath
11+
.project
12+
.settings
13+
.springBeans
14+
.sts4-cache
15+
16+
### IntelliJ IDEA ###
17+
.idea
18+
*.iws
19+
*.iml
20+
*.ipr
21+
22+
### NetBeans ###
23+
/nbproject/private/
24+
/nbbuild/
25+
/dist/
26+
/nbdist/
27+
/.nb-gradle/
28+
build/
29+
30+
### VS Code ###
31+
.vscode/
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
https://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
*/
19+
20+
import java.io.File;
21+
import java.io.FileInputStream;
22+
import java.io.FileOutputStream;
23+
import java.io.IOException;
24+
import java.net.URL;
25+
import java.nio.channels.Channels;
26+
import java.nio.channels.ReadableByteChannel;
27+
import java.util.Properties;
28+
29+
public class MavenWrapperDownloader {
30+
31+
/**
32+
* Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
33+
*/
34+
private static final String DEFAULT_DOWNLOAD_URL =
35+
"https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.4.2/maven-wrapper-0.4.2.jar";
36+
37+
/**
38+
* Path to the maven-wrapper.properties file, which might contain a downloadUrl property to
39+
* use instead of the default one.
40+
*/
41+
private static final String MAVEN_WRAPPER_PROPERTIES_PATH =
42+
".mvn/wrapper/maven-wrapper.properties";
43+
44+
/**
45+
* Path where the maven-wrapper.jar will be saved to.
46+
*/
47+
private static final String MAVEN_WRAPPER_JAR_PATH =
48+
".mvn/wrapper/maven-wrapper.jar";
49+
50+
/**
51+
* Name of the property which should be used to override the default download url for the wrapper.
52+
*/
53+
private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl";
54+
55+
public static void main(String args[]) {
56+
System.out.println("- Downloader started");
57+
File baseDirectory = new File(args[0]);
58+
System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath());
59+
60+
// If the maven-wrapper.properties exists, read it and check if it contains a custom
61+
// wrapperUrl parameter.
62+
File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH);
63+
String url = DEFAULT_DOWNLOAD_URL;
64+
if(mavenWrapperPropertyFile.exists()) {
65+
FileInputStream mavenWrapperPropertyFileInputStream = null;
66+
try {
67+
mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile);
68+
Properties mavenWrapperProperties = new Properties();
69+
mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream);
70+
url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url);
71+
} catch (IOException e) {
72+
System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'");
73+
} finally {
74+
try {
75+
if(mavenWrapperPropertyFileInputStream != null) {
76+
mavenWrapperPropertyFileInputStream.close();
77+
}
78+
} catch (IOException e) {
79+
// Ignore ...
80+
}
81+
}
82+
}
83+
System.out.println("- Downloading from: : " + url);
84+
85+
File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH);
86+
if(!outputFile.getParentFile().exists()) {
87+
if(!outputFile.getParentFile().mkdirs()) {
88+
System.out.println(
89+
"- ERROR creating output direcrory '" + outputFile.getParentFile().getAbsolutePath() + "'");
90+
}
91+
}
92+
System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
93+
try {
94+
downloadFileFromURL(url, outputFile);
95+
System.out.println("Done");
96+
System.exit(0);
97+
} catch (Throwable e) {
98+
System.out.println("- Error downloading");
99+
e.printStackTrace();
100+
System.exit(1);
101+
}
102+
}
103+
104+
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
105+
URL website = new URL(urlString);
106+
ReadableByteChannel rbc;
107+
rbc = Channels.newChannel(website.openStream());
108+
FileOutputStream fos = new FileOutputStream(destination);
109+
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
110+
fos.close();
111+
rbc.close();
112+
}
113+
114+
}

‎.mvn/wrapper/maven-wrapper.jar

47.2 KB
Binary file not shown.

‎.mvn/wrapper/maven-wrapper.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip

‎README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#Netty WebSocket入门demo
2+
3+
#### 什么是WebSocket:
4+
是一种H5协议规范,通过握手机制客户端与服务器之间就能够建立一个类似Tcp的连接,从而方便客户端与服务器之间的通信。
5+
它是一种解决客户端与服务端实时通信而产生的技术:WebSocket本质是一种基于TCP协议,先通过Http/Https发一个特殊的Http请求进行握手,握手后会创建一个用于交换数据的TCP链接,之后客户端和服务端使用该TCP链接进行实时通信。当WebSocket的客户端和服务端握手后 建立通信后,就不再需要之前的http请求参与。
6+
7+
#### WebSocket的优点:
8+
1. 节省通信开销,之前WebServer实现通信,都使用轮询(每隔特定时间间隔浏览器自动发送Http请求,去获取服务端的响应)该情况下,需要不停的向服务器发送请求,而HttpRequest的handler很长,请求包含真正的数据可能很小,会占用很多额外的带宽和服务器资源。
9+
2. 服务器主动传送数据给客户端,在给定时间,服务器和客户端在任意时刻相互推送信息,浏览器(客户端)和服务器只需要做一个握手的动作。建立连接后,服务器可主动传数据给客户端,客户端也可以随意向服务端传数据。交换数据时所携带的头信息很小。
10+
3. 实时通信:WebSocket不仅限于Ajax方式通信。ajax方式需要浏览器发起请求。而WebSocket技术 服务端和客户端可以彼此相互推送信息,从而实现实时通信。
11+
12+
13+
#### WebSocket建立连接过程:
14+
1、客户端发起握手请求。
15+
2、服务端响应请求。
16+
3、建立连接。
17+
详细流程:
18+
建立一个WebSocket连接,客户端或浏览器首先向服务器发送一个特殊的Http请求(携带一些附加头信息)Upgrade:websocket,服务端解析附加头信息,产生应答消息,然后响应给客户端,之后客户端就与服务端建立响应的链接。
19+
20+
#### WebSocket生命周期:
21+
1、打开事件:端点上建立新链接时,该事件是先于其他任何事件发生之前。该事件发生会产生三部分信息。
22+
1.1、创建WebSocket Session对象:用于表示已经建立好的链接
23+
1.2、配置对象:包含配置端点的信息。
24+
1.3、一组路径参数,用于打开节点握手时,WebSocket端入栈匹配的URI
25+
2、消息事件:主要是接收WebSocket对话中,另一端发送的消息。链接上的消息将会有三种形式抵达客户端。
26+
2.1、文本消息 用String处理
27+
2.2、二进制消息 用byteBuffer或者byte[]处理
28+
2.3、pong消息 用Java WebSocket API中的pong.message接口的实例来处理
29+
3、错误事件:WebSocket链接或者端点发生错误时产生。可以处理入栈消息时发生的各种异常。入栈消息可能产生的三种异常。
30+
3.1、WebSocket建立链接时发生错误:SessionException类型
31+
3.2、WebSocket试图将入栈消息解码成开发人员使用的对象时 EncodeException类型
32+
3.3、WebSocket端点的其他方法运行时产生的错误,WebSocket实现将记录端点操作过程中产生的任何异常
33+
4、关闭事件:WebSocket链接端点关闭,做一些清理工作,可以由参与连接的任意一个端点发出。
34+
35+
#### WebSocket如何关闭链接:
36+
1、服务器关闭底层TCP链接
37+
2、客户端发起TCP Close
38+
底层的TCP ,正常情况下应该首先由服务器关闭 ,在异常情况下客户端可以发起TCP Close。
39+
流程:当服务器被指示关闭WebSocket链接时,服务端会发起一个TCP Close操作, 客户端应该等待服务器的TCP Close
40+
41+
42+
用法:
43+
1. 启动SpringBoot服务,访问:http://localhost:8080/websocket
44+
2. 启动MainApp主程序,然后刷新页面,就可以进行发送数据了

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /