一.创建springboot项目:
二.配置:
1.添加依赖:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.netty.springboot</groupId>
<artifactId>netty-springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>netty-springboot</name>
<description>netty project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--添加static和templates的依赖/html-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<!-- <exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions> -->
</dependency>
<!-- websocket -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.57</version>
</dependency>
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--对象与json相互转换-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<!-- <resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>application.yml</include>
</includes>
</resource>
</resources> -->
</build>
</project>
2.配置aplication.yml:
server:
port: 8084 # 这里可以自定
spring:
thymeleaf:
prefix: classpath:/templates/
redis: # 这里可以自选
host: 127.0.0.1
port: 6379
password:
3.创建类,如下图结构,具体代码就不都放上来了:
4.以上例子资源:…
https://download.youkuaiyun.com/download/retank/11905952
5.补充说明:使用方式为启动项目,复制两个socket 出来,放任意目录(也copy js文件,然后改链接地址,保证能正常使用 jQuery 文件),将两个socket 改变不同身份ID,如下图:
如上改了,就可以使用了:如下图
6.对方不在线,缓存信息(我用的redis,即使项目关掉再重启,信息还在),对方上线后发送:
原来有个bug,现在需要改的代码如下:
技术交流QQ群-595892258