/* * Client.prototype.disconnect = function (disconnectCallback, headers) { * if (headers == null) { * headers = {}; * } * this._transmit("DISCONNECT", headers); * this.ws.onclose = null; * this.ws.close(); * this._cleanUp(); * return typeof disconnectCallback === "function" ? disconnectCallback() : void 0; * }; */ public void Send(string destination, StompHeader headers, string body) { if (body == null) { body = ""; } headers.Destination = destination; this.Transmit(CommandEnum.SEND, headers, body); }
public Frame(CommandEnum command, StompHeader headers, string body) { this.Command = command; if (headers != null) { this.Headers = headers; } this.Body = body; }
public void Nack(string messageId, string subscription, StompHeader headers = null) { if (headers == null) { headers = new Stomp.StompHeader(); } headers.Set("message-id", messageId); headers.Subscription = subscription; this.Transmit(CommandEnum.NACK, headers); }
/* javascript源代码。注意:js中WebSocket是new的时候就open的(不过也是异步的) * Client.prototype.connect = function () { * var args, errorCallback, headers, out; * args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; * out = this._parseConnect.apply(this, args); * headers = out[0], this.connectCallback = out[1], errorCallback = out[2]; * if (typeof this.debug === "function") { * this.debug("Opening Web Socket..."); * } * * this.ws.onmessage = (function (_this) { * return function (evt) { * var arr, c, client, data, frame, messageID, onreceive, subscription, unmarshalledData, _i, _len, _ref, _results; * data = typeof ArrayBuffer !== 'undefined' && evt.data instanceof ArrayBuffer ? (arr = new Uint8Array(evt.data), typeof _this.debug === "function" ? _this.debug("--- got data length: " + arr.length) : void 0, ((function () { * var _i, _len, _results; * _results = []; * for (_i = 0, _len = arr.length; _i < _len; _i++) { * c = arr[_i]; * _results.push(String.fromCharCode(c)); * } * return _results; * })()).join('')) : evt.data; * _this.serverActivity = now(); * if (data === Byte.LF) { * if (typeof _this.debug === "function") { * _this.debug("<<< PONG"); * } * return; * } * if (typeof _this.debug === "function") { * _this.debug("<<< " + data); * } * unmarshalledData = Frame.unmarshall(_this.partialData + data); * _this.partialData = unmarshalledData.partial; * _ref = unmarshalledData.frames; * _results = []; * for (_i = 0, _len = _ref.length; _i < _len; _i++) { * frame = _ref[_i]; * switch (frame.command) { * case "CONNECTED": * if (typeof _this.debug === "function") { * _this.debug("connected to server " + frame.headers.server); * } * _this.connected = true; * _this._setupHeartbeat(frame.headers); * _results.push(typeof _this.connectCallback === "function" ? _this.connectCallback(frame) : void 0); * break; * case "MESSAGE": * subscription = frame.headers.subscription; * onreceive = _this.subscriptions[subscription] || _this.onreceive; * if (onreceive) { * client = _this; * messageID = frame.headers["message-id"]; * frame.ack = function (headers) { * if (headers == null) { * headers = {}; * } * return client.ack(messageID, subscription, headers); * }; * frame.nack = function (headers) { * if (headers == null) { * headers = {}; * } * return client.nack(messageID, subscription, headers); * }; * _results.push(onreceive(frame)); * } else { * _results.push(typeof _this.debug === "function" ? _this.debug("Unhandled received MESSAGE: " + frame) : void 0); * } * break; * case "RECEIPT": * _results.push(typeof _this.onreceipt === "function" ? _this.onreceipt(frame) : void 0); * break; * case "ERROR": * _results.push(typeof errorCallback === "function" ? errorCallback(frame) : void 0); * break; * default: * _results.push(typeof _this.debug === "function" ? _this.debug("Unhandled frame: " + frame) : void 0); * } * } * return _results; * }; * })(this); * this.ws.onclose = (function (_this) { * return function () { * var msg; * msg = "Whoops! Lost connection to " + _this.ws.url; * if (typeof _this.debug === "function") { * _this.debug(msg); * } * _this._cleanUp(); * return typeof errorCallback === "function" ? errorCallback(msg) : void 0; * }; * })(this); * return this.ws.onopen = (function (_this) { * return function () { * if (typeof _this.debug === "function") { * _this.debug('Web Socket Opened...'); * } * headers["accept-version"] = Stomp.VERSIONS.supportedVersions(); * headers["heart-beat"] = [_this.heartbeat.outgoing, _this.heartbeat.incoming].join(','); * return _this._transmit("CONNECT", headers); * }; * })(this); * }; */ private void SendStompConnectCmd(StompHeader headers) { if (headers == null) { headers = new Stomp.StompHeader(); } if (!headers.ContainsKey("accept-version")) { headers["accept-version"] = SUPPORTED_VERSIONS; } if (!headers.ContainsKey("heart-beat")) { headers["heart-beat"] = heartbeat_outgoing + "," + heartbeat_incoming; } this.Transmit(CommandEnum.CONNECT, headers); }
/* * Client.prototype._setupHeartbeat = function (headers) { * var serverIncoming, serverOutgoing, ttl, v, _ref, _ref1; * if ((_ref = headers.version) !== Stomp.VERSIONS.V1_1 && _ref !== Stomp.VERSIONS.V1_2) { * return; * } * _ref1 = (function () { * var _i, _len, _ref1, _results; * _ref1 = headers['heart-beat'].split(","); * _results = []; * for (_i = 0, _len = _ref1.length; _i < _len; _i++) { * v = _ref1[_i]; * _results.push(parseInt(v)); * } * return _results; * })(), serverOutgoing = _ref1[0], serverIncoming = _ref1[1]; * if (!(this.heartbeat.outgoing === 0 || serverIncoming === 0)) { * ttl = Math.max(this.heartbeat.outgoing, serverIncoming); * if (typeof this.debug === "function") { * this.debug("send PING every " + ttl + "ms"); * } * this.pinger = Stomp.setInterval(ttl, (function (_this) { * return function () { * _this.ws.send(Byte.LF); * return typeof _this.debug === "function" ? _this.debug(">>> PING") : void 0; * }; * })(this)); * } * if (!(this.heartbeat.incoming === 0 || serverOutgoing === 0)) { * ttl = Math.max(this.heartbeat.incoming, serverOutgoing); * if (typeof this.debug === "function") { * this.debug("check PONG every " + ttl + "ms"); * } * return this.ponger = Stomp.setInterval(ttl, (function (_this) { * return function () { * var delta; * delta = now() - _this.serverActivity; * if (delta > ttl * 2) { * if (typeof _this.debug === "function") { * _this.debug("did not receive server activity for the last " + delta + "ms"); * } * return _this.ws.close(); * } * }; * })(this)); * } * }; */ public void Connect(StompHeader headers, Action <Frame> connectCallback, Action <Frame> failedCallback = null) { Callbacks.ConnectSuccess = connectCallback; Callbacks.Error = failedCallback; this.SockJs.OnError += (sender, args) => { Console.WriteLine(args.Exception.Message); }; if (this.SockJs.State == WebSocketState.Open) { connectCallback(new Frame()); } else {//先要将WebSocket打开,才能发送Stomp连接命令 this.Status = StompStatus.Connecting; this.Debug("Opening Web Socket for stomp connect..."); this.SockJs.Open(); } }
/* * Client.prototype.send = function (destination, headers, body) { * if (headers == null) { * headers = {}; * } * if (body == null) { * body = ''; * } * headers.destination = destination; * return this._transmit("SEND", headers, body); * }; */ public void Subscribe(string destination, Action <Frame> callback, StompHeader headers = null) { if (headers == null) { headers = new StompHeader(); } if (!headers.ContainsKey("id")) { headers.Set("id", "sub-" + this.Counter++); } headers.Destination = destination; this.Subscriptions.Add(new Stomp.Subscription() { Id = headers.Get("id"), Callback = callback, Destination = destination }); this.Transmit(CommandEnum.SUBSCRIBE, headers); }
private void Transmit(CommandEnum command, StompHeader headers, string body = null) { string msg = Frame.Marshall(command, headers, body); this.Debug(msg); while (true) { if (msg.Length > this.maxWebSocketFrameSize) { this.SockJs.Send(msg.Substring(0, this.maxWebSocketFrameSize)); msg = msg.Substring(this.maxWebSocketFrameSize); this.Debug("remaining = " + msg.Length); } else { this.SockJs.Send(msg); return; } } }
public static string Marshall(CommandEnum command, StompHeader headers, string body) { var frame = new Frame(command, headers, body); return(frame.ToString() + NULL); }
public void Disconnect(Action disconnectCallback, StompHeader headers) { Callbacks.Disconnected = disconnectCallback; this.Transmit(CommandEnum.DISCONNECT, headers); this.SockJs.Close(); }