In the development of a large-scale chat application, it is often necessary to develop multiple application services, one can guarantee the reliability of the service, and the other can increase the user load. But it is complicated to develop such an application system, after all, the same group of users will actually be in different server access, This kind of information forwarding and processing is a more troublesome thing. But through the Smartroute subscription mechanism, this series of things has become simple.
According to the objective situation in the design time is generally only a function first, for large-scale applications are often late-stage expansion. However, the design of cluster-based expansion requires much work and planning. However, using Smartroute can be very simple, Because you do not need to work on design and planning unnecessarily, you can interact across server clusters. You may feel that this is unlikely, but the following example gives you a sense of Smartroute's intelligence.
For the above scenario design needs to consider more than to consider the node and node communication, but also to consider the user's Landing node, how the message route forwarding and how to increase the node needs to cut into the cluster and other complex technology. And Smartroute does not need to install any services and configuration to automatically complete, Let's take a look at some of the more complicated things to see if you use Smartroute.
Cluster service-side implementation
It must be a complicated job to start with the architecture planning and design, but what about the design after joining Smartroute?
private static IServer Mserver; static void Main (string[] args) {Route.addloghandler (new Consoleloghandler (Logtype.debug | Logtype.error | Logtype.fatal | Logtype.info | Logtype.none | Logtype.warn)); Route.DefaultNode.Open (); Mserver = Serverfactory.createtcp (); Mserver.handler = new program (); Mserver.port = 8012; Mserver.open (); System.Threading.Thread.Sleep (-1); } public void Connect (Beetle.Express.IServer server, Beetle.Express.ChannelConnectEventArgs e) { Tcpsubscribehandler tcpsub = new Tcpsubscribehandler (); Tcpsub. Channel = E.channel; SmartRoute.Protocol.Packet Packet = new SmartRoute.Protocol.Packet (); Packet. Channel = E.channel; Packet. Receive = onmessagereceive; E.channel.package = packet; Route.subscribe (E.channel.id, tcpsub); } public void disposed (beEtle. Express.iserver server, Beetle.Express.ChannelEventArgs e) {route.unsubscribe (e.channel.id); UnRegister Ureg = new UnRegister (); Ureg. IP = E.channel.endpoint.tostring (); Ureg. Name = E.channel.name; Ureg. SendTo (s = S.mode (Receivemode.noteq), e.channel.id); private void Onmessagereceive (object sender, Beetle.Express.PackageReceiveArgs e) {IMessage me Ssage = (IMessage) e.message; Switch (message. MessageType) {case "register": Register REG = message. Getbody<register> (); E.channel.name = Reg. Name; Reg. IP = E.channel.endpoint.tostring (); Reg. SendTo (s = S.mode (Receivemode.all), e.channel.id); Break Case "Speak": Speak Speak = message. Getbody<speak> (); Speak. User = new Register (); Speak. User.Name = E.channel.name; Speak. User.ip = E.channel.endpoint.tostring (); Speak. SendTo (s = S.mode (Receivemode.noteq), e.channel.id); Break } }
You are not mistaken, the above code is a simple cluster-based chat Service Gateway, it seems to be more than writing a common network program is more simple. Let's take a look at the main code.
sign up for subscription
When the gateway receives access, it registers a subscription route.subscribe (E.channel.id, tcpsub) with the node, while the subscription's ID connects to the unique identity of the current service, and the subscription implementation is a TCP connection subscription.
Cancel Subscription
Unsubscribe Route.unsubscribe (E.channel.id) When the gateway accepts the disconnection;
Send Message
And when the gateway receives the message, the message is sent out, Reg. SendTo (s = S.mode (Receivemode.all), e.channel.id) is delivered to all subscriptions, speak. SendTo (s = S.mode (Receivemode.noteq), e.channel.id) are delivered to all subscriptions not currently channel.id
Smart Build Node Cluster
If this service only runs an instance within the current LAN, it is a normal server, but if the node discovers that there are other nodes in the network running, it will find the other side and form a communication cluster. When a cluster is assembled, the subscription information for each node is synchronized to a different node, and each node is directly connected to each other without a central service. When a node receives a message, it will find the corresponding subscription, and if it is a native subscription, it will call the subscription directly, and if it is subscribed to the other node, the message is posted to the corresponding node, and the subscription is called by the corresponding node.
A single-node dynamic line effectMulti-node Operation effect
Once the node discovers that there are other nodes in the network, it discovers the cluster and synchronizes the subscription, and once the cluster is set up, it can interact with the information through the nodes.
Summarize
In fact, the function of a chat service is not so simple, often have halls, groups and so on. In fact, using smartrote to handle this information forwarding is very simple. You can create a subscription service node for the lobby, the group, and then send the messages to the relevant subscriptions, and then the services find the specific subscribers and then distribute them. So Smartrote's subscription seems to be a minimalist feature that enables many unexpected features, such as Ftp,http and MQ, and so on, to be implemented in a subscription and registered with unified integrated distribution in Smartroute.
Download the full sample code
Smartroute Integrated Chat Communication cluster