1.注册GoEasy
官网:http://goeasy.io
官方文档:https://www.goeasy.io/cn/doc/server/publish.html
2.获取Appkey
2.1.官网注册账号
账号:178784507@qq.com 密码:…
2.2.新建一个免费的application,其中application name就是下面的channel,这时,系统会分配给你appkey
应用分免费版的和收费版(免费版最多只能创建2个应用),我这里用的是免费版。创建完应用之后如下图。
其中。appkey是验证用户的有效性的唯一标识。系统会生成两个keys,一个Super key和一个Subscribe key;它们的区别在于前者既可以订阅又可以推送,但后者只能用于订阅。
App keys:
2.3.导入SDK
Javascript:
1 <script type="text/javascript" src="http(s)://<CDN Host>/goeasy.js"></script>
2 <script type="text/javascript">
3 var goEasy = new GoEasy({
4 appkey: "您的app key"
5 });
6 goEasy.subscribe({
7 channel:"myChannel",
8 onMessage:function(message){
9 alert(message.content);
10 }
11 });
12 </script>
注意:
- 请不要将goeasy.js下载到本地,GoEasy动态为不同浏览器提供不同内容的goeasy.js,使用下载到本地的goeasy.js,将会导致某些浏览器 不能发送和接收消息。
- 如果您的应用程序需要支持Windows XP用户, 请务必使用HTTP来获取js文件,GoEasy将于2016年9月22日升级所有 SSL证书至SHA-2,届时起Windows XP用户将无法通过HTTPS连接GoEasy。
Java
方式一: 手动下载sdk
goeasy-sdk.jar
下载依赖包: gson-2.3.1.jar, slf4j-api-1.7.2.jar
方式二: Maven
添加goeasy仓库到您的pom.xml文件:
<repositories>
<repository>
<id>goeasy</id>
<name>goeasy</name>
<url>
http://maven.goeasy.io/content/repositories/releases/
</url>
</repository>
</repositories>
然后添加依赖:
<dependencies>
<dependency>
<groupId>io.goeasy</groupId>
<artifactId>goeasy-sdk</artifactId>
<version>0.3.8</version>
</dependency>
</dependencies>
3.代码实现
<%@ page pageEncoding="UTF-8" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="path" value="${pageContext.request.contextPath}" />
<script src="${path}/bootstrap/js/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.goeasy.io/goeasy-1.0.3.js"></script>
<script>
var goEasy = new GoEasy({
host:'hangzhou.goeasy.io', //应用所在的区域地址: 【hangzhou.goeasy.io |singapore.goeasy.io】
appkey: "BC-df0a45499f274b2bae29aes50a6a12dc9", //替换为您的应用appkey
});
//发送消息
$(function(){
var nputMsg;
/*订阅消息*/
goEasy.subscribe({
channel: "hehehe",
onMessage: function (message) {
//获取接收的内容
var send = message.content;
//接收的是不是我发的内容
if(nputMsg==send){
//是 不处理
}else{
//不是追加处理
//渲染发送页面
var msgDiv=("<div style=';width:auto;height: 30px;'>" +
"<div style='float:left;background-color: #9a9afb;border-radius: 12px'>"+message.content+"</div>" +
"</div>");
//每次发消息追加
$("#showMsg").append(msgDiv);
}
}
});
//点击发送按钮发送消息
$("#sendMsg").click(function(){
//获取输入框输入的内容
var content=$("#content").val();
//给InputMsg赋值
nputMsg=content;
//发送消息
goEasy.publish({
channel: "hehehe", //替换为您自己的channel
message: content, //替换为您想要发送的消息内容
onSuccess:function(){
//清空输入框
$("#content").val("");
//渲染发送页面
var msgDiv=("<div style=';width:auto;height: 30px;'>" +
"<div style='float:right;background-color: #acdd4a;border-radius: 12px'>"+content+"</div>" +
"</div>");
//每次发消息追加
$("#showMsg").append(msgDiv);
},
onFailed: function (error) {
alert("消息发送失败,错误编码:"+error.code+" 错误信息:"+error.content);
}
});
});
});
</script>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div align="center" >
<div style="width: 600px;height:700px;border: 3px #acdd4a solid">
<%--消息展示框--%>
<div id="showMsg" style="width: 594px;height:600px;border: 3px #ccaadd solid" ></div>
<%--消息展示框--%>
<div style="width: 594px;height:88px;border: 3px #a6e1ec solid" >
<%--输入框--%>
<textarea id="content" style="width: 500px;height:88px;"></textarea>
<%--提交按钮--%>
<button id="sendMsg" name="aaa" style="width: 70px;height:40px;background-color: pink">发送</button>
</div>
</div>
</div>
</body>
</html>