uniapp引用外部js_UNIAPP使用SockJS和Stomp不完全指北

前言

如果只是初步使用websocket进行一些简单的业务逻辑交互,此文章可以直接跳过,它将不会给你带来太多的帮助。

如果只是初步使用websocket进行一些简单的业务逻辑交互,此文章可以直接跳过,它将不会给你带来太多的帮助。

如果只是初步使用websocket进行一些简单的业务逻辑交互,此文章可以直接跳过,它将不会给你带来太多的帮助。

重要的事情说三遍

本文内容较多,主要是一些试错的全过程,并且全程在小程序开发工具上测试

因为我司在PC端的项目使用的SockJS+Stomp构建的websocket消息体系,所以在使用UNIAPP构建小程序时应保留该特性,来保证项目的统一完整性。

啥玩意?UNIAPP的websocket不香吗?不好吗?用个锤子的SockJS+Stomp?

我相信,大多数朋友拿到这个需求的时候第一反应是这个,但是又有什么办法呢,我仅仅是一个为了生活而屈服的码农而已。

开始

刚开始,想着无非是使用两个库,通过NPM的方式加载两个库,然后根据文档一步步的配置不就OK了吗?这不是So easy,直接动手。

1、通过NPM引用两个JS库

具体操作请参照UNIAPP官网NPM支持

通过Yarn或者NPM安装sockjs-client以及stompjs

yarn add sockjs-client stompjs

很容易就可以将这两个库加入我们的小程序中,是不是很简单,接下来就是按部就班的引用和配置了。我们在App.vue中加入如下代码:

import Stomp from 'stompjs'
import SockJS from "sockjs-client";

onLoad() {
    
  const websocket = new SockJS("ws://websocket");
  const stompClient = Stomp.over(websocket);
  stompClient.connect(
    {},
    (res) => {
    
        console.log(res)
    },
    (error) => {
    
        console.error('error', error)
    }
  );
}

心想着,这么简单的活我要了一整天,今天看来又能愉快的划水摸鱼了,开开心心的在开发工具中运行我们的程序。

f4c96f72b9522ff4bc5338e536761b81.png

然后我一脸懵的看着这个调试工具,我的socket连接呢???算了,一次不成功也没啥,好事多磨嘛,先去console里面看看有没有什么异常。

c545f99343de3b5e614bf247bc09fda9.png

UNIAPP里面调用原生JS的DOM操作???作死也不是这样作死的好吗?谁都知道小程序里面没有原生的DOM操作!!!不过,额,好像这两个库也没说支持小程序把....作孽啊!!!

看了看手机里面某呗还款的提醒,哎......一分钱ÿ

  • 6
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3, SockJS, and STOMP are all technologies commonly used in web development. Vue 3 is a JavaScript framework for building user interfaces, SockJS is a WebSocket emulation library, and STOMP (Simple Text Oriented Messaging Protocol) is a protocol used for real-time communication between clients and servers. In the context of Vue 3, you can use SockJS and STOMP to establish a WebSocket connection between your Vue application and a server. This allows for real-time data exchange between the client and the server. To use Vue 3 with SockJS and STOMP, you will need to install the necessary dependencies. You can do this using a package manager like npm or yarn. Here's an example of how you can install them: ```bash npm install sockjs-client stompjs ``` Once the dependencies are installed, you can import them in your Vue component and create a WebSocket connection. Here's a basic example: ```javascript import SockJS from 'sockjs-client'; import { Stomp } from '@stomp/stompjs'; export default { mounted() { const socket = new SockJS('http://your-server-url'); const client = Stomp.over(socket); client.connect({}, () => { // Connection successful client.subscribe('/topic/some-topic', (message) => { // Handle incoming messages console.log(message.body); }); }); }, }; ``` In this example, we create a new SockJS instance and establish a connection to the server URL specified. We then create a Stomp client over this socket and use the `connect()` method to establish a connection. Once the connection is established, we can subscribe to specific topics and handle incoming messages. This is just a basic example, and there are many other features and configurations you can explore with Vue 3, SockJS, and STOMP. I recommend referring to the official documentation of each library for more in-depth information and usage examples.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值