diff --git a/Front-End-Testing/chat90.html b/Front-End-Testing/chat90.html index 3611b0f..24dedb9 100644 --- a/Front-End-Testing/chat90.html +++ b/Front-End-Testing/chat90.html @@ -54,7 +54,7 @@ window.WebSocket = window.MozWebSocket; } if (window.WebSocket) { - socket = new WebSocket("ws://192.168.56.1:8090/ws"); + socket = new WebSocket("ws://192.168.1.121:8090/ws"); socket.onmessage = function(event) { var ta = document.getElementById('responseText'); ta.value = ta.value + '\n' + event.data diff --git a/pom.xml b/pom.xml index e750597..61c83fa 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,12 @@ 4.0.0 - + + org.springframework.boot + spring-boot-starter-parent + 2.0.2.RELEASE + + com.github.UncleCatMySelf InChat 1.1.3 @@ -75,11 +80,11 @@ jedis ${redis.clients.version} - - org.slf4j - slf4j-log4j12 - ${slf4j.version} - + + + + + com.alibaba fastjson @@ -100,6 +105,31 @@ commons-lang3 ${commons.lang3.version} + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-jdbc + + + mysql + mysql-connector-java + runtime + + + org.projectlombok + lombok + true + + + junit junit diff --git a/src/main/java/com/github/unclecatmyself/application.java b/src/main/java/com/github/unclecatmyself/application.java index 1775260..7378460 100644 --- a/src/main/java/com/github/unclecatmyself/application.java +++ b/src/main/java/com/github/unclecatmyself/application.java @@ -2,21 +2,23 @@ import com.github.unclecatmyself.auto.ConfigFactory; import com.github.unclecatmyself.auto.InitServer; -import com.github.unclecatmyself.users.DataBaseServiceImpl; -import com.github.unclecatmyself.users.FromServerServiceImpl; -import com.github.unclecatmyself.users.MyInit; -import com.github.unclecatmyself.users.VerifyServiceImpl; +import com.github.unclecatmyself.users.*; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Create by UncleCatMySelf in 22:49 2019\1\4 0004 */ +@SpringBootApplication public class application { public static void main(String[] args) { + SpringApplication.run(application.class, args); ConfigFactory.initNetty = new MyInit(); ConfigFactory.inChatVerifyService = new VerifyServiceImpl(); ConfigFactory.inChatToDataBaseService = new DataBaseServiceImpl(); ConfigFactory.fromServerService = FromServerServiceImpl.TYPE2; + ConfigFactory.textData = new UserTextData(); // ConfigFactory.RedisIP = "192.168.0.101"; // ConfigFactory.RedisIP = "192.168.192.132"; diff --git a/src/main/java/com/github/unclecatmyself/auto/ConfigFactory.java b/src/main/java/com/github/unclecatmyself/auto/ConfigFactory.java index d0195eb..afb5920 100644 --- a/src/main/java/com/github/unclecatmyself/auto/ConfigFactory.java +++ b/src/main/java/com/github/unclecatmyself/auto/ConfigFactory.java @@ -4,6 +4,7 @@ import com.github.unclecatmyself.bootstrap.data.InChatToDataBaseService; import com.github.unclecatmyself.bootstrap.verify.InChatVerifyService; import com.github.unclecatmyself.common.bean.InitNetty; +import com.github.unclecatmyself.task.TextData; /** * 默认配置工厂 @@ -11,7 +12,7 @@ */ public class ConfigFactory { - /** Redis的ip地址,这你不会不知道吧? */ + /** Redis的ip地址 */ public static String RedisIP; /** 用户校验伪接口 */ @@ -26,4 +27,6 @@ public class ConfigFactory { /** InChat项目配置 */ public static InitNetty initNetty; + public static TextData textData; + } diff --git a/src/main/java/com/github/unclecatmyself/bootstrap/AbstractBootstrapServer.java b/src/main/java/com/github/unclecatmyself/bootstrap/AbstractBootstrapServer.java index 4756e2d..970ff58 100644 --- a/src/main/java/com/github/unclecatmyself/bootstrap/AbstractBootstrapServer.java +++ b/src/main/java/com/github/unclecatmyself/bootstrap/AbstractBootstrapServer.java @@ -10,6 +10,7 @@ import com.github.unclecatmyself.common.ssl.SecureSocketSslContextFactory; import com.github.unclecatmyself.common.utils.SslUtil; import com.github.unclecatmyself.task.DataAsynchronousTask; +import com.github.unclecatmyself.users.UserTextData; import io.netty.channel.ChannelPipeline; import io.netty.handler.codec.http.HttpObjectAggregator; import io.netty.handler.codec.http.HttpRequestDecoder; @@ -60,7 +61,7 @@ protected void initHandler(ChannelPipeline channelPipeline, InitNetty serverBea } intProtocolHandler(channelPipeline,serverBean); channelPipeline.addLast(new IdleStateHandler(serverBean.getHeart(),0,0)); - channelPipeline.addLast(new DefaultHandler(new HandlerServiceImpl(new DataAsynchronousTask(ConfigFactory.inChatToDataBaseService),ConfigFactory.inChatVerifyService))); + channelPipeline.addLast(new DefaultHandler(new HandlerServiceImpl(new DataAsynchronousTask(ConfigFactory.inChatToDataBaseService),ConfigFactory.inChatVerifyService,ConfigFactory.textData))); } private void intProtocolHandler(ChannelPipeline channelPipeline,InitNetty serverBean){ diff --git a/src/main/java/com/github/unclecatmyself/bootstrap/channel/HandlerServiceImpl.java b/src/main/java/com/github/unclecatmyself/bootstrap/channel/HandlerServiceImpl.java index d2d259a..22764ab 100644 --- a/src/main/java/com/github/unclecatmyself/bootstrap/channel/HandlerServiceImpl.java +++ b/src/main/java/com/github/unclecatmyself/bootstrap/channel/HandlerServiceImpl.java @@ -13,11 +13,14 @@ import com.github.unclecatmyself.common.bean.SendInChat; import com.github.unclecatmyself.common.bean.vo.SendServerVO; import com.github.unclecatmyself.common.constant.Constans; +import com.github.unclecatmyself.task.TextData; import com.google.gson.Gson; import com.github.unclecatmyself.bootstrap.channel.ws.WsChannelService; import com.github.unclecatmyself.bootstrap.verify.InChatVerifyService; import com.github.unclecatmyself.task.DataAsynchronousTask; import io.netty.channel.Channel; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; import io.netty.handler.codec.http.FullHttpMessage; import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; import io.netty.util.CharsetUtil; @@ -39,11 +42,14 @@ public class HandlerServiceImpl extends HandlerService { private final WsChannelService websocketChannelService = new WebSocketChannelService(); - private final DataAsynchronousTask dataAsynchronousTask; + private DataAsynchronousTask dataAsynchronousTask; - public HandlerServiceImpl(DataAsynchronousTask dataAsynchronousTask,InChatVerifyService inChatVerifyService) { + private TextData textData; + + public HandlerServiceImpl(DataAsynchronousTask dataAsynchronousTask,InChatVerifyService inChatVerifyService,TextData textData) { this.dataAsynchronousTask = dataAsynchronousTask; this.inChatVerifyService = inChatVerifyService; + this.textData = textData; } @@ -86,12 +92,25 @@ public boolean login(Channel channel, Map maps) { public void sendMeText(Channel channel, Map maps) { Gson gson = new Gson(); channel.writeAndFlush(new TextWebSocketFrame( - gson.toJson(inChatBackMapService.sendMe((String) maps.get(Constans.VALUE))))); - try { - dataAsynchronousTask.writeData(maps); - } catch (Exception e) { - e.printStackTrace(); - } + gson.toJson(inChatBackMapService.sendMe((String) maps.get(Constans.VALUE))))).addListener( + new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture future) throws Exception { + if (future.isSuccess()){ + dataAsynchronousTask.writeData(maps); + textData.writeData(maps); + } else { + future.cause().printStackTrace(); + future.channel().close(); + } + } + } + ); +// try { +// dataAsynchronousTask.writeData(maps); +// } catch (Exception e) { +// e.printStackTrace(); +// } } @Override diff --git a/src/main/java/com/github/unclecatmyself/bootstrap/handler/DefaultHandler.java b/src/main/java/com/github/unclecatmyself/bootstrap/handler/DefaultHandler.java index 3c0645f..c799238 100644 --- a/src/main/java/com/github/unclecatmyself/bootstrap/handler/DefaultHandler.java +++ b/src/main/java/com/github/unclecatmyself/bootstrap/handler/DefaultHandler.java @@ -90,7 +90,7 @@ protected void httpdoMessage(ChannelHandlerContext ctx, FullHttpRequest msg) { httpHandlerService.notFindUri(channel); break; default: - System.out.println("为匹配"+msg); + System.out.println("未匹配"+msg); break; } } diff --git a/src/main/java/com/github/unclecatmyself/common/utils/SpringContextUtils.java b/src/main/java/com/github/unclecatmyself/common/utils/SpringContextUtils.java new file mode 100644 index 0000000..e561ce1 --- /dev/null +++ b/src/main/java/com/github/unclecatmyself/common/utils/SpringContextUtils.java @@ -0,0 +1,69 @@ +package com.github.unclecatmyself.common.utils; + +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.stereotype.Component; +import org.springframework.stereotype.Service; + +import java.util.Map; + +/** + * Created by MySelf on 2019/8/20. + */ +@Component +public class SpringContextUtils implements ApplicationContextAware { + + /** + * 上下文对象实例 + */ + private static ApplicationContext applicationContext; + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + this.applicationContext = applicationContext; + } + + /** + * 获取applicationContext + * + * @return + */ + public static ApplicationContext getApplicationContext() { + return applicationContext; + } + + /** + * 通过name获取 Bean. + * + * @param name + * @return + */ + public static Object getBean(String name) { + return getApplicationContext().getBean(name); + } + + /** + * 通过class获取Bean. + * + * @param clazz + * @param + * @return + */ + public static T getBean(Class clazz) { + return getApplicationContext().getBean(clazz); + } + + /** + * 通过name,以及Clazz返回指定的Bean + * + * @param name + * @param clazz + * @param + * @return + */ + public static T getBean(String name, Class clazz) { + return getApplicationContext().getBean(name, clazz); + } + +} diff --git a/src/main/java/com/github/unclecatmyself/task/TextData.java b/src/main/java/com/github/unclecatmyself/task/TextData.java new file mode 100644 index 0000000..854daa9 --- /dev/null +++ b/src/main/java/com/github/unclecatmyself/task/TextData.java @@ -0,0 +1,12 @@ +package com.github.unclecatmyself.task; + +import java.util.Map; + +/** + * Created by MySelf on 2019/8/20. + */ +public abstract class TextData { + + public abstract void writeData(Map maps); + +} diff --git a/src/main/java/com/github/unclecatmyself/users/UserTextData.java b/src/main/java/com/github/unclecatmyself/users/UserTextData.java new file mode 100644 index 0000000..f2587a5 --- /dev/null +++ b/src/main/java/com/github/unclecatmyself/users/UserTextData.java @@ -0,0 +1,30 @@ +package com.github.unclecatmyself.users; + +import com.github.unclecatmyself.common.utils.SpringContextUtils; +import com.github.unclecatmyself.task.TextData; +import com.github.unclecatmyself.users.pojo.Test; +import com.github.unclecatmyself.users.repository.TestRepository; +import org.apache.catalina.User; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.stereotype.Component; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.Map; + +/** + * Created by MySelf on 2019/8/20. + */ +public class UserTextData extends TextData { + + private TestRepository repository = (TestRepository) SpringContextUtils.getBean(TestRepository.class); + + @Override + public void writeData(Map maps) { + Test test = new Test(); + test.setId(1); + test.setMsg("1111"); + repository.save(test); + } +} diff --git a/src/main/java/com/github/unclecatmyself/users/pojo/Test.java b/src/main/java/com/github/unclecatmyself/users/pojo/Test.java new file mode 100644 index 0000000..f77993d --- /dev/null +++ b/src/main/java/com/github/unclecatmyself/users/pojo/Test.java @@ -0,0 +1,34 @@ +package com.github.unclecatmyself.users.pojo; + +import lombok.Data; +import org.hibernate.annotations.DynamicUpdate; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import java.io.Serializable; +import java.util.Date; + +/** + * @ClassName TestUser + * @Description 框架测试用户,测试密码加密,不允许生产使用 + * @Author MySelf + * @Date 2019/7/27 11:06 + * @Version 1.0 + **/ +@Entity +@Data +@DynamicUpdate +public class Test implements Serializable { + + /**id*/ + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; + + /**信息*/ + private String msg; + + +} diff --git a/src/main/java/com/github/unclecatmyself/users/repository/TestRepository.java b/src/main/java/com/github/unclecatmyself/users/repository/TestRepository.java new file mode 100644 index 0000000..930283e --- /dev/null +++ b/src/main/java/com/github/unclecatmyself/users/repository/TestRepository.java @@ -0,0 +1,15 @@ +package com.github.unclecatmyself.users.repository; + +import com.github.unclecatmyself.users.pojo.Test; +import org.springframework.data.jpa.repository.JpaRepository; + +/** + * @InterfaceName TestUserRepository + * @Description Jpa映射层 + * @Author MySelf + * @Date 2019/7/27 11:05 + * @Version 1.0 + **/ +public interface TestRepository extends JpaRepository { + +} diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml new file mode 100644 index 0000000..70413af --- /dev/null +++ b/src/main/resources/application-dev.yml @@ -0,0 +1,8 @@ +spring: + datasource: + driver-class-name: com.mysql.jdbc.Driver + url: jdbc:mysql://192.168.192.133:3306/inchat + username: root + password: password + jpa: + show-sql: true \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 0000000..caf4dfc --- /dev/null +++ b/src/main/resources/application.yml @@ -0,0 +1,3 @@ +spring: + profiles: + active: dev \ No newline at end of file diff --git a/src/main/resources/log4j.properties b/src/main/resources/log4j.properties.md similarity index 100% rename from src/main/resources/log4j.properties rename to src/main/resources/log4j.properties.md