[置顶] 基于MQTT+WCF 实现Web 接口转即时服务

时间:2021-11-24 22:23:52

-MQTT Client
-MQTT Broker
-WCF(Base Winform)
-WebControler (Base Asp.net MVC)

  1. 场景描述
    有App+Web API 实现的系统,无法实现即时服务提醒
  2. 整体模型
    [置顶]        基于MQTT+WCF 实现Web 接口转即时服务
  3. 解决思路
    通过使用MQTT Broker的快速搭建,开发即时服务器应用 SocketAPP1,Android/IOS 其他应用使用MQTT连接服务器,只处理订阅信息。SocketAPP1 开发WCF服务,外部能调用。更正原来Web API 增加调用WCF 服务进行通知服务。SokcetApp 和Web App并在一个服务器上,Socket APP不处理数据层

代码实例:
***Android MQTT Content
NotifyClient***

try {
mesenger=(Messenger)intent.getExtras().get("Handle");
String uuid=UUID.randomUUID().toString();
uuid.replace("-","");
uuid=uuid.substring(0,23);
client = new NettyClient(uuid
);
client.setListener(new PrintingListener());
client.connect("***.***.48.239", 1883);
subscribe(topic);
client.publish("keepalive","keepalive");
mTimer.schedule(new TimerTask() {
@Override
public void run() {
try {
client.publish("keepalive","keepalive");
Log.d("Keepalive","keepalive");
} catch (Exception e) {
e.printStackTrace();
}
}
}, 0, 10 * 1000);
beInteractive();
// client.disconnect();
Message msg = Message.obtain();
msg.arg1=2;


try {
msg.obj="MQTT 服务已退出";
mesenger.send(msg);
}catch (Exception ex)
{
ex.printStackTrace();
}
}
catch (Exception ex)
{
// Toast.makeText(null,ex.getMessage(),Toast.LENGTH_SHORT).show();
ex.printStackTrace();
}

***Winform MQTT broker Winform WCF
GnatMQ***

//MQTT Broker
uPLibrary.Networking.M2Mqtt.MqttBroker server=new uPLibrary.Networking.M2Mqtt.MqttBroker();
server.Start();

//启动WCF 服务
m_Host =new ServiceHost(serviceMqtt);
m_Host.Open();
Console.WriteLine("WCF Server has opened .");

Web APP

//调用WCF 通知
ServiceReference1.ServiceMQTTClient s = new ServiceReference1.ServiceMQTTClient();

相关文章