国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

uni app Signalr 支持 微信小程序和支付寶小程序

這篇具有很好參考價值的文章主要介紹了uni app Signalr 支持 微信小程序和支付寶小程序。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

const protocal = {
	protocol: "json",
	version: 1
};

const MessageType = {
	/** Indicates the message is an Invocation message and implements the {@link InvocationMessage} interface. */
	Invocation: 1,
	/** Indicates the message is a StreamItem message and implements the {@link StreamItemMessage} interface. */
	StreamItem: 2,
	/** Indicates the message is a Completion message and implements the {@link CompletionMessage} interface. */
	Completion: 3,
	/** Indicates the message is a Stream Invocation message and implements the {@link StreamInvocationMessage} interface. */
	StreamInvocation: 4,
	/** Indicates the message is a Cancel Invocation message and implements the {@link CancelInvocationMessage} interface. */
	CancelInvocation: 5,
	/** Indicates the message is a Ping message and implements the {@link PingMessage} interface. */
	Ping: 6,
	/** Indicates the message is a Close message and implements the {@link CloseMessage} interface. */
	Close: 7,
}


export class HubConnection {

	constructor() {
		this.openStatus = false;
		this.methods = {};
		this.negotiateResponse = {};
		this.connection = {};
		this.url = "";
		this.invocationId = 0;
		this.callbacks = {};
	}


	start(url, queryString) {
		var negotiateUrl = url + "/negotiate";
		if (queryString) {
			for (var query in queryString) {
				negotiateUrl += (negotiateUrl.indexOf("?") < 0 ? "?" : "&") + (`${query}=` + encodeURIComponent(
					queryString[query]));
			}
		}

		uni.request({
			url: negotiateUrl,
			method: "post",
			async: false,
			success: res => {
				this.negotiateResponse = res.data;
				this.startSocket(negotiateUrl.replace("/negotiate", ""));
			},
			fail: res => {
				console.error(`requrst ${url} error : ${res}`);
				return;
			}
		});

	}

	startSocket(url) {
		url += (url.indexOf("?") < 0 ? "?" : "&") + ("id=" + this.negotiateResponse.connectionId);
		url = url.replace(/^http/, "ws");
		this.url = url;
		if (this.connection != null && this.openStatus) {
			return;
		}

		// #ifdef MP-WEIXIN
		this.connection = wx.connectSocket({
			url: url,
			method: "get"
		})
		console.log("微信 WEIXIN");
		// #endif

		// #ifdef MP-ALIPAY
		this.connection = my.connectSocket({
			url: url,
			method: "get"
		})
		console.log("支付寶 ALIPAY");
		// #endif


		this.connection.onOpen(res => {
			console.log(`websocket connectioned to ${this.url}`);
			this.sendData(protocal);
			this.openStatus = true;
			this.onOpen(res);
		});

		this.connection.onClose(res => {
			console.log(`websocket disconnection`);
			this.connection = null;
			this.openStatus = false;
			this.onClose(res);
		});

		this.connection.onError(res => {
			console.error(`websocket error msg: ${msg}`);
			this.close({
				reason: msg
			});
			this.onError(res)
		});

		this.connection.onMessage(res => this.receive(res));
	}


	on(method, fun) {

		let methodName = method.toLowerCase();
		if (this.methods[methodName]) {
			this.methods[methodName].push(fun);
		} else {
			this.methods[methodName] = [fun];

		}
	}

	onOpen(data) {}

	onClose(msg) {}

	onError(msg) {}


	close(data) {
		if (data) {
			this.connection.close(data);
		} else {
			this.connection.close();
		}

		this.openStatus = false;
	}

	sendData(data, success, fail, complete) {
		this.connection.send({
			data: JSON.stringify(data) + "", //
			success: success,
			fail: fail,
			complete: complete
		});
	}


	receive(data) {
		if (data.data.length > 3) {
			data.data = data.data.replace('{}', "")
		}

		var messageDataList = data.data.split("");

		//循環(huán)處理服務(wù)端信息
		for (let serverMsg of messageDataList) {
			if (serverMsg) {
				var messageData = serverMsg.replace(new RegExp("", "gm"), "")
				var message = JSON.parse(messageData);

				switch (message.type) {
					case MessageType.Invocation:
						this.invokeClientMethod(message);
						break;
					case MessageType.StreamItem:
						break;
					case MessageType.Completion:
						var callback = this.callbacks[message.invocationId];
						if (callback != null) {
							delete this.callbacks[message.invocationId];
							callback(message);
						}
						break;
					case MessageType.Ping:
						// Don't care about pings
						break;
					case MessageType.Close:
						console.log("Close message received from server.");
						this.close({
							reason: "Server returned an error on close"
						});
						break;
					default:
						console.warn("Invalid message type: " + message.type);
				}
			}
		}
	}


	send(functionName) {

		var args = [];
		for (var _i = 1; _i < arguments.length; _i++) {
			args[_i - 1] = arguments[_i];
		}

		this.sendData({
			target: functionName,
			arguments: args,
			type: MessageType.Invocation,
			invocationId: this.invocationId.toString()
		});
		this.invocationId++;
	}


	invoke(functionName) {
		var args = [];
		for (var _i = 1; _i < arguments.length; _i++) {
			args[_i - 1] = arguments[_i];
		}

		var _this = this;
		var id = this.invocationId;
		var p = new Promise(function(resolve, reject) {

			_this.callbacks[id] = function(message) {
				if (message.error) {
					reject(new Error(message.error));
				} else {
					resolve(message.result);
				}
			}

			_this.sendData({
				target: functionName,
				arguments: args,
				type: MessageType.Invocation,
				invocationId: _this.invocationId.toString()
			}, null, function(e) {
				reject(e);
			});

		});
		this.invocationId++;
		return p;

	}

	invokeClientMethod(message) {
		var methods = this.methods[message.target.toLowerCase()];
		if (methods) {
			methods.forEach(m => m.apply(this, message.arguments));
			if (message.invocationId) {
				// This is not supported in v1. So we return an error to avoid blocking the server waiting for the response.
				var errormsg = "Server requested a response, which is not supported in this version of the client.";
				console.error(errormsg);
				this.close({
					reason: errormsg
				});
			}
		} else {
			console.warn(`No client method with the name '${message.target}' found.`);
		}
	}
}

使用方法文章來源地址http://www.zghlxwxcb.cn/news/detail-595425.html

			var Hub = require("@/config/Signalr.js")
			let connection = new Hub.HubConnection();
			//注意:這里的apiHost不是wss或ws,是https或http
			connection.start(`https://15826q7q9.oicp.vip/ws`, {
				access_token: '如果有token則填寫'
			});

			connection.onOpen = res => {
				uni.showToast({
					content: '成功開啟連接'
				});
			}

			connection.on('system', (res) => {
				uni.showToast({
					content: res
				});
				console.log(res);
			});

			connection.on("receive", res => {
				uni.showToast({
					content: '消息' + res.msg
				});

			})

到了這里,關(guān)于uni app Signalr 支持 微信小程序和支付寶小程序的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • uni-app使用支付寶小程序開發(fā)者工具開發(fā)釘釘小程序

    uni-app使用支付寶小程序開發(fā)者工具開發(fā)釘釘小程序

    一、添加運行配置 在項目的 package.json 文件中添加以下配置 配置完畢后在 HBuilderX 中會顯示運行釘釘小程序按鈕 ?二、設(shè)置釘釘小程序開發(fā)工具路徑(支付寶小程序開發(fā)者工具) ?三、通過 HBuilderX? 把 uniapp 項目編譯成釘釘小程序 編譯成功后會自動打開釘釘小程序開發(fā)工具,編

    2024年02月11日
    瀏覽(31)
  • 跳轉(zhuǎn)微信小程序和支付寶小程序

    參考鏈接 獲取微信小程序 URL Scheme 1.1 獲取小程序連接 這里需要獲取長期有效的 Scheme,方式如下: 聯(lián)系小程序開發(fā)者 其他渠道 示例 小程序 Scheme : 測試地址,可以打開小程序,只是打開后顯示已注銷 weixin://dl/business/?ticket=l92578fd8404e0d4e3e975f910fa43f3a 1.2 跳轉(zhuǎn)使用 蘋果手機中

    2024年02月06日
    瀏覽(95)
  • uniapp兼容微信小程序和支付寶小程序遇到的坑

    uniapp兼容微信小程序和支付寶小程序遇到的坑

    改為v-if。 App端和H5端支持 v-html ,微信小程序會被轉(zhuǎn)為 rich-text,其他端不支持 v-html。 解決方法:去插件市場找一個支持跨端的富文本組件。 兼容微信小程序和支付寶小程序? pages.json: 給支付寶的導(dǎo)航欄設(shè)置 透明 agent頁面: 支付寶加上 my.setNavigationBar 設(shè)置標(biāo)題文字即可,

    2024年02月15日
    瀏覽(24)
  • 【uniapp】將微信小程序的代碼兼容支付寶小程序(持續(xù)更新)

    【uniapp】將微信小程序的代碼兼容支付寶小程序(持續(xù)更新)

    目前本身就有一套完善的微信小程序(兼容h5)的代碼,現(xiàn)在的需求是將它編譯成支付寶小程序,做好兼容的處理,以便后續(xù)接入支付寶服務(wù)商,在這里簡單記錄一下目前發(fā)現(xiàn)的把微信小程序編譯成支付寶小程序的問題和解決方案。 建議配合其他人的記錄一起看,這里只是我

    2024年02月09日
    瀏覽(93)
  • 【uni-app】只支持在微信小程序運行的 導(dǎo)入外部3d模型

    【uni-app】只支持在微信小程序運行的 導(dǎo)入外部3d模型

    uniapp 導(dǎo)入3d模型,在微信小程序端運行。只支持在微信小程序端使用,若要支持 h5 和 APP端,可以查看這篇,點擊這里 只導(dǎo)入了3d模型,未設(shè)置讓模型動。 glb 格式 (1)首先文件 下載適配微信小程序的 three.js 地址:https://github.com/wechat-miniprogram/threejs-miniprogram (2)導(dǎo)入文件

    2023年04月08日
    瀏覽(23)
  • uniapp 之 微信小程序、支付寶小程序 對于自定義導(dǎo)航欄的不同

    uniapp 之 微信小程序、支付寶小程序 對于自定義導(dǎo)航欄的不同

    目錄 前言? 微信小程序 代碼? 支付寶小程序 首頁配置文件 二級菜單頁面? 配置 總結(jié)? 不同 相同 ?小程序都是 uni-app 寫的 不是原生 pages.json文件中配置 重點:?\\\"navigationStyle\\\": \\\"custom\\\",??// 導(dǎo)航欄樣式 ?首頁 vue文件 template ?script ?備注: ?height:是膠囊的高度 首頁配置文件

    2024年02月15日
    瀏覽(30)
  • 一碼多端,一個二維碼適用微信小程序,支付寶小程序,h5頁面

    一碼多端,一個二維碼適用微信小程序,支付寶小程序,h5頁面

    最近公司研發(fā)自己的一個小程序,因為是線下樹牌,涉及到掃碼這個問題,但是掃碼又分三個端,瀏覽器掃碼,微信掃一掃,支付寶掃碼,做這個需求也是遇到了很多坑,在此記錄一下 1.掃碼進入微信小程序 首先登錄微信公眾平臺,鏈接 https://mp.weixin.qq.com/ ?原本此處會有一

    2024年02月08日
    瀏覽(92)
  • uniapp 藍(lán)牙連接設(shè)備 下發(fā)命令 監(jiān)聽藍(lán)牙與設(shè)備的連接狀態(tài)(兼容 微信小程序和支付寶小程序)

    1:創(chuàng)建藍(lán)牙需要調(diào)用的Api文件?ly.js 2 在頁面中使用index.vue?

    2024年04月14日
    瀏覽(22)
  • uni-app Vue3實現(xiàn)一個酷炫的多功能音樂播放器支持微信小程序后臺播放

    uni-app Vue3實現(xiàn)一個酷炫的多功能音樂播放器支持微信小程序后臺播放

    本文存在多張gif演示圖,建議在 wifi 環(huán)境下閱讀?? 最近在做網(wǎng)易云音樂微信小程序開源項目的時候,關(guān)于 播放器功能 參考了一些成熟的微信小程序,如 網(wǎng)易云音樂小程序 和 QQ音樂小程序 ,但是發(fā)現(xiàn)這些 小程序端 的播放器相對于 APP端 來說較簡單,只支持一些基礎(chǔ)功能,

    2024年01月24日
    瀏覽(103)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包