Skip to content

Commit

Permalink
初始化notify
Browse files Browse the repository at this point in the history
  • Loading branch information
shuzheng committed Jul 3, 2018
1 parent e9b08db commit 248b57b
Show file tree
Hide file tree
Showing 18 changed files with 679 additions and 0 deletions.
25 changes: 25 additions & 0 deletions zheng-notify/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<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>

<groupId>com.zheng</groupId>
<artifactId>zheng-notify</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>

<name>zheng-notify</name>
<url>http://www.zhangshuzheng.cn</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>
</properties>

<modules>
<module>zheng-notify-sdk</module>
<module>zheng-notify-server</module>
</modules>
</project>
83 changes: 83 additions & 0 deletions zheng-notify/zheng-notify-client/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>管理监控中心</title>
<style>
#main{width:100%;height:400px;margin:0 auto;}
.namespace{width:100%;height:400px;background:#000;}
.title{width:100%;height:40px;font-size:0;background:#666;}
.input,.button{float:left;height:40px;border:none;background:none;outline:none;color:#fff;}
.input{width:25%;text-indent:10px;font-weight:bold;}
.button{width:10%;cursor:pointer;}
#token{width:20%;}
#env{background:rgb(194, 53, 49);}
.messages{width:100%;height:340px;overflow-y:auto;font-size:12px;line-height:2em;color:#fff;}
.messages li{list-style-type:none;}
.messages .connect{color:#00cc00;}
.messages .warn{color:#cccc00;}
.messages .disconnect{color:#cc0000;}
table{width:100%;border-collapse:collapse;border:solid #ccc;border-width:1px 0 0 1px}
table th,table td{border:solid #ccc;border-width:0 1px 1px 0;padding:14px;text-align:center}
</style>
</head>
<body>
<div id="main">
<div class="namespace">
<div class="title">
<select id="env" class="input">
<option value="http://localhost:8882">开发环境</option>
</select>
<input id="namespace" class="input" type="text" placeholder="namespace" value=""/>
<input id="token" class="input" type="text" placeholder="token" value=""/>
<input id="connect" class="button" type="button" value="连接"/>
<input id="send" class="button" type="button" value="发言"/>
<input id="disconnect" class="button" type="button" value="断开"/>
</div>
<div class="messages">
<ul id="list">
</ul>
</div>
</div>
</div>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/socket.io/2.1.1/socket.io.js"></script>
<script type="text/javascript">
$(function() {
// SocketIO客户端
var socket;
$('#connect').click(function() {
// 获取token,请求业务系统接口
var token = $('#token').val();
// 创建socketio连接
socket = io.connect($('#env').val() + $('#namespace').val() + '?token=' + token);
socket.on('connect', function() {
$('#list').prepend('<li class="connect">已连接服务器!</li>');
});
socket.on('message', function(data, ackServerCallback) {
$('#list').prepend('<li class="data">收到服务器消息:' + JSON.stringify(data) + '</li>');
if (ackServerCallback) {
ackServerCallback();
}
});
socket.on('connect_error', function(error) {
$('#list').prepend('<li class="warn">连接服务器失败!</li>');
console.log(error);
});
socket.on('disconnect', function() {
$('#list').prepend('<li class="disconnect">已断开服务器!</li>');
});
$('#send').click(function() {
socket.emit('message', {userName: 'userName', message: new Date()});
});
$('#disconnect').click(function() {
socket.close();
});
// 防止重复连接
$(this).attr('disabled','disabled');
});
});
</script>
</body>
</html>
60 changes: 60 additions & 0 deletions zheng-notify/zheng-notify-sdk/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<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>

<parent>
<groupId>com.zheng</groupId>
<artifactId>zheng-notify</artifactId>
<version>1.0.0</version>
</parent>

<artifactId>zheng-notify-sdk</artifactId>
<packaging>jar</packaging>

<name>zheng-notify-sdk</name>
<url>http://www.zhangshuzheng.cn</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
</dependencies>

<build>
<finalName>zheng-notify-sdk</finalName>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<skipTests>true</skipTests>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.zheng.notify.sdk;

/**
* 统一返回结果类
* Created by shuzheng on 2017/2/18.
*/
public class BaseResult {

/**
* 状态码:1成功,其他为失败
*/
public int code;

/**
* 成功为success,其他为失败原因
*/
public String message;

/**
* 数据结果集
*/
public Object data;

public BaseResult(int code, String message, Object data) {
this.code = code;
this.message = message;
this.data = data;
}

public int getCode() {
return code;
}

public void setCode(int code) {
this.code = code;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public Object getData() {
return data;
}

public void setData(Object data) {
this.data = data;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.zheng.notify.sdk;

/**
* 全局常量
* Created by shuzheng on 2018/6/28.
*/
public class NotifyConstants {

// 通知类型:广播
public static final String PUBLISH_TYPE_BROADCAST = "broadcast";

// 通知类型:主题
public static final String PUBLISH_TYPE_TOPICS = "topics";

// 通知类型:点对点
public static final String PUBLISH_TYPE_CLIENTS = "clients";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.zheng.notify.sdk;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.nio.charset.Charset;

/**
* 推送工具类
* Created by shuzheng on 2018/6/28.
*/
public class NotifyHelper {

/**
* 推送消息
* @param url
* @param publishDto
* @return
*/
public BaseResult publish(String url, PublishDto publishDto) {
HttpPost httpPost = null;
try {
HttpClient httpClient = new DefaultHttpClient();

httpPost = new HttpPost(url);
httpPost.setHeader("Content-type", "application/json; charset=utf-8");

HttpEntity httpEntity = new StringEntity(JSONObject.toJSONString(publishDto), "utf-8");
httpPost.setEntity(httpEntity);

HttpResponse httpResponse = httpClient.execute(httpPost);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
HttpEntity resEntity = httpResponse.getEntity();
if (resEntity != null) {
String result = EntityUtils.toString(resEntity, Charset.forName("utf-8"));
return JSONObject.parseObject(result, BaseResult.class);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (httpPost != null) {
httpPost.releaseConnection();
}
}
return new BaseResult(-1, "error", "publish error");
}

public static void main(String[] args) {
String url = "http://localhost:8881/notify/publish";
PublishDto publishDto = new PublishDto();
publishDto.setType(NotifyConstants.PUBLISH_TYPE_BROADCAST);
publishDto.setNamespace("/demo");
publishDto.setAccessKey("1");
publishDto.setPayload("hi");
BaseResult result = new NotifyHelper().publish(url, publishDto);
System.out.println(result.message);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.zheng.notify.sdk;


/**
* 推送消息
* Created by shuzheng on 2018/6/20.
*/
public class PublishDto {

// 推送类型:"broadcast/topics/clients" <=> 广播、按主题、点对点
private String type;

// 推送命名空间
private String namespace;

// 鉴权key
private String accessKey;

// 推送订阅主题的订阅者
private String[] topics;

// 推送客户端
private String[] clients;

// 消息体
private Object payload;

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getNamespace() {
return namespace;
}

public void setNamespace(String namespace) {
this.namespace = namespace;
}

public String[] getTopics() {
return topics;
}

public void setTopics(String[] topics) {
this.topics = topics;
}

public String[] getClients() {
return clients;
}

public void setClients(String[] clients) {
this.clients = clients;
}

public Object getPayload() {
return payload;
}

public void setPayload(Object payload) {
this.payload = payload;
}

public String getAccessKey() {
return accessKey;
}

public void setAccessKey(String accessKey) {
this.accessKey = accessKey;
}

}
Loading

0 comments on commit 248b57b

Please sign in to comment.