Uniapp uses GoEasy to implement websocket real-time communication

As the most popular mobile terminal development technology recently, Uniapp is a set of codes that can be packaged into Android/iOS apps and small programs of various platforms. It is not the most convenient but more convenient.

GoEasy has been listed on the DCloud Uniapp plugin market for more than a month. It has received praise and praise from many Uniapp developers. Xiaobian bows to everyone across the screen. Xiaobian and the GoEasy team will continue to work hard and continue to provide Uniapp developers with The simplest and most elegant API, stable, high-speed and reliable instant messaging service.

During this time, I also received some questions from many friends? for example:

1. What are the differences and advantages between GoEasy and Uniapp websocket API?

Uniapp's official websocket API is mainly used to communicate with your websocket service, so the premise of using Uniapp websocket is to first build your own websocket service, and then communicate with it. This is a purely technical API. After establishing a network connection, there is still a lot of work to be done by yourself, such as:

  • You need to implement the heartbeat mechanism by yourself to maintain the network connection and judge the network connection status of the client;
  • You need to realize automatic reconnection after disconnection;
  • You need to maintain the message list yourself to ensure that the message can be re-sent after a network disconnection and reconnection;
  • You need to maintain a client list yourself;
  • ...

Wait for a lot of detailed and complicated work, such as websocket security mechanism and performance optimization;

In addition, there is a lot of work on the server side that needs to be done by yourself. If you are interested in building a websocket yourself, you can refer to this technical sharing " Several issues that must be considered when building a websocket message push service "

GoEasy is a mature and stable websocket enterprise-level PAAS service platform. Developers do not need to consider the construction of the websocket server. They only need a few lines of code to easily realize the communication between the client and the client, and between the server and the client. Websocket communication does not need to consider performance, security, and high-availability cluster issues. Just focus fully on developing your own business features.

GoEasy has built-in features such as heartbeat, network disconnection and reconnection, message re-sending, historical messages and client-side offline reminders, which are necessary in websocket. Developers do not need to build their own websocket services to deal with cluster high availability, security and performance issues. GoEasy has been running stably for 5 years, supports tens of millions of concurrency, and has successfully supported many important activities of well-known companies. Its security and reliability are time-tested.

2. What scenarios is GoEasy mainly used in Uniapp development?

First of all, technically, all the technologies android, iOS and applet supported by Uniapp are perfectly supported by GoEasy.

In terms of application scenarios, GoEasy can perfectly support all scenarios that require websocket communication:

  • Chat, IM, live barrage, user online and offline reminder, online user list
  • Scan code to order, scan code to log in, scan code to pay, scan code to sign in, scan code to print
  • Event reminder, work order, order real-time reminder
  • Online auction, online ordering, online seat selection real-time data display, real-time monitoring large screen, financial real-time market display, equipment monitoring system
  • Real-time location tracking, real-time delivery tracking, real-time logistics tracking
  • Remote drawing board, telemedicine, games, remote online teaching
  • ...

3. Why is GoEasy's documentation so simple? It's so simple that I don't know how to use it

Simple isn't it? From the first day of development, GoEasy has put the pursuit of API simplicity as our focus. Strictly control the number of interfaces is to reduce the learning cost of developers, in fact, it is to make you happy! But this does not affect GoEasy's perfect support for all websocket instant messaging needs.

Today, Xiaobian will teach you how to use GoEasy to implement a web instant messaging demo in the shortest time under Uniapp.

This demo has passed the real machine test of applet, iOS and Android. The complete source code has been uploaded to github. After downloading, you only need to replace the appkey in the code with your own common key to experience it. download link:

https://gitee.com/goeasy-io/GoEasyDemo-Uniapp-Helloworld

1. Get appkey

Register an account on GoEasy official website ( http://www.goeasy.io/ ), create an app, and get your appkey

GoEasy provides two types of appkeys:

Common key: It can receive messages and send messages. The biggest difference from Subscriber Key is that it has write permission and can send messages. It is suitable for client and server development with message sending requirements.

Subscriber key: can only receive messages, but cannot send messages. The biggest difference from Common Key is that it has no write permission and can only receive messages. Can be used for some clients that do not need to send.

2. Get the GoEasy SDK

Two ways:

DCloud Uniapp plugin market download: https://ext.dcloud.net.cn/plugin?id=1334

You can also download it directly from the goeasy official website: https://www.goeasy.io/cn/doc/client/get-goeasy-js.html

3. Initialize the GoEasy object

In main.js, goeasy is initialized as the global object of Uniapp, so that all pages can be easily called, and it also avoids repeated new GoEasy objects on multiple pages.

Enter different hosts according to the region you selected when you created the application in the GoEasy background. If you selected Hangzhou when you created the goeasy application, then host: "hangzhou.goeasy.io". Choose Singapore, host: "singapore.goeasy.io".

If most of your users are in China, remember to choose Hangzhou when creating an application for faster communication speed.

// 在main.js中将goeasy初始化为全局对象,所有页面都能方便的调用,也避免多个页面反复new GoEasy对象
Vue.prototype.$goEasy = new GoEasy({
host: "hangzhou.goeasy.io",
appkey: "my_appkey", //替换为您的应用appkey
    onConnected: function() {
      console.log('连接成功!')
    },
    onDisconnected: function() {
      console.log('连接断开!')
    },
    onConnectFailed: function(error) {
      console.log('连接失败或错误!')
    }
});

4. The uniapp side receives the message

this.$goEasy.subscribe({
    channel: "my_channel", //替换为您自己的channel
    onMessage: function (message) {
        alert("Channel:" + message.channel + " content:" + message.content);
    }
});

Many friends will ask where the channel comes from, how to create it, and what should be passed in?

Set according to your business needs, channel can be any string, except that it cannot contain spaces, and it is not recommended to use Chinese, there are no restrictions, as long as it is consistent with the sender of the message, you can receive the message. The channel can be the uuid of your live broadcast room, or the unique identifier of a user, which can be defined arbitrarily. The channel does not need to be created and can be discarded whenever it is used.

5. The uniapp side sends a message

When sending, it should be noted that the channel must be exactly the same as the subscriber's channel, otherwise it cannot be received.

this.$goEasy.publish({
    channel: "my_channel", //替换为您自己的channel
    message: "Hello, GoEasy!" //替换为您想要发送的消息内容
});

Download the source code of this code:

https://gitee.com/goeasy-io/GoEasyDemo-Uniapp-Helloworld

The applet specifically emphasizes:

If you need to package it as a WeChat applet, you need to configure the socket legal domain name on the WeChat official account platform, otherwise it cannot be used. Specific steps:

Visit https://mp.weixin.qq.com, enter WeChat public platform | Mini Program -> Settings -> Development Settings -> Server Domain Name

Socket legal domain name -> add the address of goeasy: wx-hangzhou.goeasy.io (remember the beginning of wx-)

If you selected the Singapore region when you created the GoEasy app, add the address: wx-singapore.goeasy.io

Question time:

1. Can my server send messages? Which languages ​​are supported?

Of course, any language can send messages by calling goeasy's Rest API. At the same time, for everyone's convenience, GoEasy's official documentation also includes Java, C#, NodeJS, PHP, Ruby, Python and other common languages. The code to call the REST API, Get more details here: https://www.goeasy.io/cn/doc/server/publish.html

2. Can GoEasy send pictures, voice and video?

Of course, you can send the file by pushing the file path.

According to industry practice, regardless of MSN, WeChat, or QQ, the usual practice for pictures and videos is to only push the file path, but not the file itself. If you pay attention, when you accept pictures and videos, after receiving the message, you can't watch it for a while, because when you send it, only the path is sent.

GoEasy official website: https://www.goeasy.io/

GoEasy series of tutorials:

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324202326&siteId=291194637