简单mpush长连接实现
2019-10-22 本文已影响0人
jia々
1.mpush服务搭建,根据官网http://mpush.mydoc.io/?t=134336服务部署搭建redis服务,zk服务
2.服务端sdk接入,根据文档配置,发送send实现如下
public class MpushUtils {
private static MpushUtils mpushUtils;
static Logger log = LoggerFactory.getLogger(MpushUtils.class);
private final static PushSender sender = PushSender.create();
public static void send(String userId, PushMsg msg, String msgId) throws Exception {
FutureTask<PushResult> future = null;
if (!sender.isRunning()) {
sender.start();
}
msg.setMsgId("msgId" + msgId);
PushContext context = PushContext.build(msg)
.setAckModel(AckModel.AUTO_ACK)
.setUserId(userId)
//.setUserIds(userIds)
.setBroadcast(false)
.setTimeout(2000)
.setCallback(new PushCallback() {
//推送成功后的回调
@Override
public void onResult(PushResult result) {
log.info("\n\n" + result);
}
});
future = sender.send(context);
log.info("推送结果===========>" + future.toString());
}
3.MPushClient(Java)是一个java写的客户端具体接入mpush服务如下
public class MpushUtil {
private static final Logger logger = LogManager.getLogger(MpushUtil.class);
private static final String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iViRxcTkey/B792VZEhuHjQvA3cAJgx2Lv8GnX8NIoShZtoCg3Cx6ecs+VEPD2fBcg2L4JK7xldGpOJ3ONEAyVsLOttXZtNXvyDZRijiErQALMTorcgi79M5uVX9/jMv2Ggb2XAeZhlLD28fHwIDAQAB";
public static Client client;
public static void connectAndbind(String userId) {
String serverHost = "192.168.80.182";//部署好的mpush服务ip
String cacheDir = MpushUtil.class.getResource("/").getFile();
ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor();
ClientListener listener = new L(scheduledExecutor);
client = ClientConfig
.build()
.setPublicKey(publicKey)
//.setAllotServer(allocServer) //未使用alloc服务
.setServerHost(serverHost)
.setServerPort(3000) //mpush服务端口号默认是3000
.setDeviceId("deviceId")
.setUserId(userId)
.setSessionStorageDir(cacheDir)
.setLogger(new DefaultLogger())
.setLogEnabled(true)
.setEnableHttpProxy(true)
.setClientListener(listener)
.create();
client.start();
}
public static class L implements ClientListener {
private final ScheduledExecutorService scheduledExecutor;
boolean flag = true;
public L(ScheduledExecutorService scheduledExecutor) {
this.scheduledExecutor = scheduledExecutor;
}
@Override
public void onConnected(Client client) {
System.out.println("================>connected");
flag = true;
}
@Override
public void onDisConnected(Client client) {
System.out.println("================>disconnected");
flag = false;
}
@Override
public void onHandshakeOk(final Client client, final int heartbeat) {
System.out.println("=================>handshakeOk");
System.out.println();
scheduledExecutor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
client.healthCheck();
}
}, 10, 10, TimeUnit.SECONDS);
//client.push(PushContext.build("test"));
}
@Override
public void onKickUser(String deviceId, String userId) {
System.out.println("==============================>onkickUser");
}
/**
* <B>方法名称:</B><BR>
* <B>概要说明:推送消息监听</B><BR>
*
* @see com.mpush.api.ClientListener#onReceivePush(com.mpush.api.Client, byte[])
*/
@Override
public void onReceivePush(Client client, byte[] content) {
System.out.println("======================>onReceivePush");
//用于监听后台服务推送信息
}
}
public static void main(String[] args) {
connectAndbind("12345");
//client.stop();//关闭进程后要将长连接关闭不然可能导致心跳重连的时候出错
}
}
注意:在客户端启动连接的时候本人是报错
PushSender推送建立连接超时
错误如下:io.netty.channel.ConnectionTimeOutException:connection timed out: /xxx.xxx.xxx.xxx:3001
解决方式:
a.) 首先检查ip是否正确 telnet xxx.xxx.xxx.xxx 3001是否通
b.) 如果ip不正确增加配置mp.net.local-ip="指定具体的ip"
解决方法是在搭建的mpush服务中修改
mp.net.local-ip="192.168.80.182" //本地ip, 默认取第一个网卡的本地IP