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); }
public static Frame UnmarshallSingle(string data) { int divider = Math.Max(data.IndexOf("" + LF + LF), 0); var headerLines = data.Substring(0, divider).Split(LF); CommandEnum command = (CommandEnum)Enum.Parse(typeof(CommandEnum), headerLines.First()); StompHeader headers = new Stomp.StompHeader(); for (int i = 1, len = headerLines.Length; i < len; i++) { string line = headerLines[i]; int idx = line.IndexOf(':'); headers[line.Substring(0, idx).Trim()] = line.Substring(idx + 1).Trim(); } string body = ""; int start = divider + 2; if (headers.ContainsKey("content-length")) { int utf8Len = int.Parse(headers["content-length"]); //注意,stomp是以utf8字节来算字符串长度的!!!所以下面截取body要用UTF8字节处理。 //但是stomp.js的js源码中是直接用data.substring(start, start + utf8Len)处理的,虽然js容错性好不会报错。不过一般utf8长度肯定大于字符长度,不会少截断,貌似直接截到结束也没什么关系?详细的得去研究Stomp协议body的格式 body = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(data.Substring(start)), 0, utf8Len); } else { Char?chr = null; int i, j, len; for (i = j = start, len = data.Length; start <= len ? j <len : j> len; i = start <= len ? ++j : --j) { chr = data[i]; if (chr == NULL) { break; } body += chr; } } return(new Frame(command, headers, body)); }