using System.Text; using NetMQ.Core; namespace NetMQ.Sockets { /// /// An XPublisherSocket is a NetMQSocket intended to be used as the XPub in the XPub/XSub pattern. /// The intended usage is for serving, together with a matching XSubscriberSocket, /// as a stable intermediary between a PublisherSocket and it's SubscriberSockets. /// public class XPublisherSocket : NetMQSocket { /// /// Create a new XPublisherSocket and attach socket to zero or more endpoints. /// /// List of NetMQ endpoints, separated by commas and prefixed by '@' (to bind the socket) or '>' (to connect the socket). /// Default action is bind (if endpoint doesn't start with '@' or '>') /// var socket = new XPublisherSocket(">tcp://127.0.0.1:5555,>tcp://127.0.0.1:55556"); public XPublisherSocket(string? connectionString = null) : base(ZmqSocketType.Xpub, connectionString, DefaultAction.Bind) { } /// /// Create a new XPublisherSocket based upon the given . /// /// the SocketBase to create the new socket from internal XPublisherSocket(SocketBase socketHandle) : base(socketHandle) { } /// /// In case of socket set to manual mode will subscribe the last subscriber to the topic /// /// a string specifying the Topic to subscribe to public virtual void Subscribe(string topic) { SetSocketOption(ZmqSocketOption.Subscribe, topic); } /// /// In case of socket set to manual mode will subscribe the last subscriber to the topic /// /// a string specifying the Topic to subscribe to /// the character-Encoding to use when converting the topic string internally into a byte-array public virtual void Subscribe(string topic, Encoding encoding) { Subscribe(encoding.GetBytes(topic)); } /// /// In case of socket set to manual mode will subscribe the last subscriber to the topic /// /// a byte-array specifying the Topic to subscribe to public virtual void Subscribe(byte[] topic) { SetSocketOption(ZmqSocketOption.Subscribe, topic); } /// /// In case of socket set to manual mode will unsubscribe the last subscriber from a topic /// /// a string specifying the Topic to unsubscribe from public virtual void Unsubscribe(string topic) { SetSocketOption(ZmqSocketOption.Unsubscribe, topic); } /// /// In case of socket set to manual mode will unsubscribe the last subscriber from a topic /// /// a string specifying the Topic to unsubscribe from /// the character-Encoding to use when converting the topic string internally into a byte-array public virtual void Unsubscribe(string topic, Encoding encoding) { Unsubscribe(encoding.GetBytes(topic)); } /// /// In case of socket set to manual mode will unsubscribe the last subscriber from a topic /// /// a byte-array specifying the Topic to unsubscribe from public virtual void Unsubscribe(byte[] topic) { SetSocketOption(ZmqSocketOption.Unsubscribe, topic); } /// /// Publisher sockets generally send a welcome-message to subscribers to give an indication that they have successful subscribed. /// This method clears that message, such that none is sent. /// public void ClearWelcomeMessage() { SetSocketOption(ZmqSocketOption.XPublisherWelcomeMessage, null); } /// /// Publisher sockets send a welcome-message to subscribers to give an indication that they have successful subscribed. /// This method is how you set the text of that welcome-message. /// /// a string denoting the new value for the welcome-message /// the character-Encoding to use when converting the topic string internally into a byte-array public void SetWelcomeMessage(string welcomeMessage, Encoding encoding) { SetWelcomeMessage(encoding.GetBytes(welcomeMessage)); } /// /// Publisher sockets send a welcome-message to subscribers to give an indication that they have successful subscribed. /// This method is how you set the text of that welcome-message. The Encoding is assumed to be ASCII. /// /// a string denoting the new value for the welcome-message public void SetWelcomeMessage(string welcomeMessage) { SetWelcomeMessage(Encoding.ASCII.GetBytes(welcomeMessage)); } /// /// Publisher sockets send a welcome-message to subscribers to give an indication that they have successful subscribed. /// This method is how you set the text of that welcome-message. The Encoding is assumed to be ASCII. /// /// a byte-array denoting the new value for the welcome-message public void SetWelcomeMessage(byte[] welcomeMessage) { SetSocketOption(ZmqSocketOption.XPublisherWelcomeMessage, welcomeMessage); } } }