C# websocket real-time message push
The implementation steps are as follows:
1. Get the GoEasy appkey.
Register an account on the goeasy official website and create a new app. After the app is created, the system will automatically generate two keys for the app , one can be used to receive and push (supper key) , and the other can only be used to Receive (subscriber key).
2. The client subscribes to a channel.
a. Introduce goeasy.js on the client side , js address: http://cdn.goeasy.io/goeasy.js
It should be noted that goeasy.js cannot be downloaded locally, because goeasy will provide different js according to different browsers . There are corresponding instructions on the official website.
b. Subscription code
var goEasy = new GoEasy({appkey: 'subscriber key'});// I use the subscriber key here, because I don't need to push any messages on my client, so there is no need to use the supper key
goEasy. subscribe({
channel: 'csdnNotification',
onMessage: function(message){
alert('Meessage received:'+message.content);// Received the push message
}
});
3. Front-end push and background push
3.1. The front desk pushes to the same channel
On the page where goeasy.js is introduced , directly call goEasy.publish('csdnNotification', ' your friend 222 is online '); the usage is similar to subscription.
3.2. Push to the same channel in the background . Implemented with the restful API provided by GoEasy
API url: http://goeasy.io/goeasy/publish
Method : Post
parameter list:
channel: 'csdnNotification' // Must be consistent with the subscribed channel , otherwise the client will not receive messages. Therefore, we can also control which clients can receive according to the channel .
appkey: 'supper key' // The supper key must be used here
content: 'blabla…..' // Content to be pushed
I hope everyone has to help.