初始化与建立连接
在使用GoEasy进行消息的接收和发送之前,需要先初始化GoEasy对象, 并建立与GoEasy的连接。
初始化GoEasy对象
let goeasy = GoEasy.getInstance({
host:"hangzhou.goeasy.io", //若是新加坡区域:singapore.goeasy.io
appkey:"您的common key",
modules:['im']//根据需要,传入'im’或‘pubsub’,或数组方式同时传入
});
挂载GoEasy为全局对象
对于单页面web应用,为了便于在整个应用中随时随地的调用GoEasy
对象,强烈建议将GoEasy
对象初始化为全局对象。
Vue和Uniapp
在main.js
里将goeasy对象
挂载为全局对象。
Vue.prototype.goeasy = GoEasy.getInstance({
host:"hangzhou.goeasy.io", //若是新加坡区域:singapore.goeasy.io
appkey:"您的common key",
modules:['im']//根据需要,传入'im’或'pubsub',或数组方式同时传入
});
微信小程序
在app.js
里将goeasy对象
挂载为全局对象。
App({
onLaunch: function () {
wx.goEasy = GoEasy.getInstance({
host:'hangzhou.goeasy.io',//若是新加坡区域:singapore.goeasy.io
appkey: 'BC-xxxx',// common key
modules:['im']//根据需要,传入'im’或'pubusub',或数组方式同时传入
});
}
})
建立连接
一个网页、一个app或者小程序需要,且只需要
建立一个连接即可,在成功断开连接之前,不需要,也不能
反复建立连接。
提示
微信小程序特别提醒:
如果需要在小程序里使用GoEasy,需要登录微信公众平台->微信小程序开发设置->服务器域名, 添加以下所有5个socket合法域名:
wss://1hangzhou.goeasy.io
wss://2hangzhou.goeasy.io
wss://3hangzhou.goeasy.io
wss://4hangzhou.goeasy.io
wss://5hangzhou.goeasy.io
//建立连接
goeasy.connect({
id:"001", //im必填,最大长度60字符
data:{"avatar":"/www/xxx.png","nickname":"Neo"}, //必须是一个对象,im必填,最大长度300字符,显示在会话列表中
onSuccess: function () { //连接成功
console.log("GoEasy connect successfully.") //连接成功
},
onFailed: function (error) { //连接失败
console.log("Failed to connect GoEasy, code:"+error.code+ ",error:"+error.content);
},
onProgress:function(attempts) { //连接或自动重连中
console.log("GoEasy is connecting", attempts);
}
});