netty-确定客户端和微信端的沟通方式
This commit is contained in:
37
netty-model/pom.xml
Normal file
37
netty-model/pom.xml
Normal file
@@ -0,0 +1,37 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<parent>
|
||||
<groupId>smartvenue</groupId>
|
||||
<artifactId>smartvenue-parent</artifactId>
|
||||
<version>2.0.0</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>netty-model</artifactId>
|
||||
<version>${smartvenue.version}</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.netty</groupId>
|
||||
<artifactId>netty-all</artifactId>
|
||||
<version>4.1.10.Final</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>1.7.33</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-core</artifactId>
|
||||
<version>2.13.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>2.13.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.sv.netty.config;
|
||||
|
||||
/**
|
||||
* 会话中存储的客户端对象
|
||||
*
|
||||
* @author peakren
|
||||
* @since 16/05/2017 11:09 PM
|
||||
*/
|
||||
public class ChannelParam {
|
||||
|
||||
/**
|
||||
* 设备ip
|
||||
*/
|
||||
private String clientIp;
|
||||
|
||||
/**
|
||||
* 设备编号
|
||||
*/
|
||||
private String deviceName;
|
||||
|
||||
/**
|
||||
* 设备所在场馆
|
||||
*/
|
||||
private Integer venueId;
|
||||
|
||||
public ChannelParam(String clientIP) {
|
||||
this.clientIp = clientIP;
|
||||
}
|
||||
|
||||
public String getClientIp() {
|
||||
return clientIp;
|
||||
}
|
||||
|
||||
public void setClientIp(String clientIp) {
|
||||
this.clientIp = clientIp;
|
||||
}
|
||||
|
||||
public String getDeviceName() {
|
||||
return deviceName;
|
||||
}
|
||||
|
||||
public void setDeviceName(String deviceName) {
|
||||
this.deviceName = deviceName;
|
||||
}
|
||||
|
||||
public Integer getVenueId() {
|
||||
return venueId;
|
||||
}
|
||||
|
||||
public void setVenueId(Integer venueId) {
|
||||
this.venueId = venueId;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.sv.netty.config;
|
||||
|
||||
import io.netty.util.AttributeKey;
|
||||
|
||||
public interface NettyConstant {
|
||||
String SERVER_IP = "120.27.209.4";
|
||||
Integer SERVER_PORT = 56791;
|
||||
|
||||
/**
|
||||
* session中存储终端发送的额外参数
|
||||
*/
|
||||
public static AttributeKey<ChannelParam> CHANNEL_PARAM = AttributeKey.newInstance("CHANNEL_PARAM");
|
||||
|
||||
public final static String SPIT_WORD = "#";
|
||||
|
||||
}
|
||||
13
netty-model/src/main/java/com/sv/netty/utils/EncodeMsg.java
Normal file
13
netty-model/src/main/java/com/sv/netty/utils/EncodeMsg.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package com.sv.netty.utils;
|
||||
|
||||
public enum EncodeMsg {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
final String MAGIC = "\r\n";
|
||||
|
||||
public String encode(Object obj) {
|
||||
return JsonUtils.encode(obj) + MAGIC;
|
||||
}
|
||||
|
||||
}
|
||||
89
netty-model/src/main/java/com/sv/netty/utils/JsonUtils.java
Normal file
89
netty-model/src/main/java/com/sv/netty/utils/JsonUtils.java
Normal file
@@ -0,0 +1,89 @@
|
||||
package com.sv.netty.utils;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerationException;
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class JsonUtils {
|
||||
|
||||
|
||||
/**
|
||||
* Logger for this class
|
||||
*/
|
||||
private static final Logger logger = LoggerFactory.getLogger(JsonUtils.class);
|
||||
|
||||
private final static ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
static {
|
||||
objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
|
||||
objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
|
||||
objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
|
||||
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
}
|
||||
|
||||
private JsonUtils() {
|
||||
}
|
||||
|
||||
public static String encode(Object obj) {
|
||||
try {
|
||||
return objectMapper.writeValueAsString(obj);
|
||||
} catch (JsonGenerationException e) {
|
||||
logger.error("encode(Object)", e); //$NON-NLS-1$
|
||||
} catch (JsonMappingException e) {
|
||||
logger.error("encode(Object)", e); //$NON-NLS-1$
|
||||
} catch (IOException e) {
|
||||
logger.error("encode(Object)", e); //$NON-NLS-1$
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将json string反序列化成对象
|
||||
*
|
||||
* @param json
|
||||
* @param valueType
|
||||
* @return
|
||||
*/
|
||||
public static <T> T decode(String json, Class<T> valueType) {
|
||||
try {
|
||||
return objectMapper.readValue(json, valueType);
|
||||
} catch (JsonParseException e) {
|
||||
logger.error("decode(String, Class<T>)", e);
|
||||
} catch (JsonMappingException e) {
|
||||
logger.error("decode(String, Class<T>)", e);
|
||||
} catch (IOException e) {
|
||||
logger.error("decode(String, Class<T>)", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将json array反序列化为对象
|
||||
*
|
||||
* @param json
|
||||
* @param typeReference
|
||||
* @return
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T decode(String json, TypeReference<T> typeReference) {
|
||||
try {
|
||||
return (T) objectMapper.readValue(json, typeReference);
|
||||
} catch (JsonParseException e) {
|
||||
logger.error("decode(String, JsonTypeReference<T>)", e);
|
||||
} catch (JsonMappingException e) {
|
||||
logger.error("decode(String, JsonTypeReference<T>)", e);
|
||||
} catch (IOException e) {
|
||||
logger.error("decode(String, JsonTypeReference<T>)", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.sv.netty.utils;
|
||||
|
||||
import com.sv.netty.config.VenueMessage;
|
||||
import io.netty.channel.Channel;
|
||||
|
||||
public enum ServerMessageUtils {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
public void sendMsg(Channel channel, VenueMessage message) {
|
||||
channel.writeAndFlush(EncodeMsg.INSTANCE.encode(message));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user