/** * Return the subscribed events of transaction & block. * @see {@link https://github.com/nebulasio/wiki/blob/master/rpc.md#subscribe} * * @param {Object} options * @param {Array|String} options.topics * @param {Function} options.onDownloadProgress - On progress callback function. Recive chunk. * * @return [eventData]{@link https://github.com/nebulasio/wiki/blob/master/rpc.md#subscribe} * * @example * var api = new Neb().api; * api.subscribe({topics: ["chain.linkBlock", "chain.pendingTransaction"]}).then(function(eventData) { * //code * }); */ //长链接,需保持线程运行 public void Subscribe(string[] topics, Action <string> callBackEvent) { RequestSubscribe request = new RequestSubscribe() { Topics = topics }; string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(request, Formatting.Indented, UnderLineSetting); beginSendRequestAsync("post", "/subscribe", jsonString, callBackEvent); }
public void Connect() { WebSocket webSocket = new WebSocket("wss://ws-feed.pro.coinbase.com"); webSocket.OnOpen += WebSocket_OnOpen; webSocket.OnMessage += WebSocket_OnMessage; webSocket.OnError += WebSocket_OnError; webSocket.OnClose += WebSocket_OnClose; RequestSubscribe req = new RequestSubscribe(); var serRecCancByGid = JsonConvert.SerializeObject(req, CCoinbaseJsonSerializer.Settings); webSocket.Connect(); System.Threading.Thread.Sleep(3000); webSocket.Send(serRecCancByGid); Console.ReadLine(); }
/// <summary> /// Subscribes the specified proxy client identifier. /// </summary> /// <param name="proxyClientId">The proxy client identifier.</param> /// <param name="topicList">The topic list.</param> /// <returns></returns> public OutputValues Subscribe(int proxyClientId, KeyValueCollection topicList) { OutputValues output = OutputValues.GetInstance(); output.Message = string.Empty; output.MessageCode = string.Empty; output.ErrorCode = 0; try { RequestSubscribe requestSubscribe = RequestSubscribe.Create(); requestSubscribe.ProxyClientId = proxyClientId; requestSubscribe.PsTopicList = topicList; if (Settings.InteractionProtocol != null && Settings.InteractionProtocol.State == ChannelState.Opened) { IMessage message = Settings.InteractionProtocol.Request(requestSubscribe); if (message != null) { switch (message.Id) { case EventAck.MessageId: EventAck eventSubscribe = (EventAck)message; logger.Info("-------------RequestSubscribe----------------"); logger.Info("ProxyClientId :" + proxyClientId); logger.Info("PsTopicList :" + topicList.ToString()); logger.Info("Name :" + eventSubscribe.Name.ToString()); logger.Info("--------------------------------------------"); logger.Trace(eventSubscribe.ToString()); output.MessageCode = "200"; output.Message = "Subscribe Successful"; break; case EventError.MessageId: EventError eventError = (EventError)message; string LoginErrorCode = Convert.ToString(eventError.ErrorCode); string LoginErrorDescription = Convert.ToString(eventError.ErrorDescription); logger.Trace(eventError.ToString()); output.ErrorCode = eventError.ErrorCode; output.MessageCode = "2002"; output.Message = "Subscribe() : " + LoginErrorDescription; break; } } else { output.MessageCode = "2001"; output.Message = "Subscribe UnSuccessful"; } } else { logger.Warn("Subscribe() : Interaction Server Protocol is Null.."); } } catch (Exception commonException) { logger.Error("Error occurred while Subscribe request " + commonException.ToString()); output.MessageCode = "2001"; output.Message = commonException.Message; } return(output); }