constructor
observer
accessToken
start
stop
sendPeerMessage
msg
(Object
): message property(sender, receiver, content, msgLocalID)sendGroupMessage
msg
(Object
): message property(sender, receiver, content, msgLocalID)onConnectState
state
:im service's connect statehandlePeerMessage
msg
(Object
): message property(sender, receiver, content, timestamp)handleMessageACK
msg
(Object
): message property(sender, receiver, content, timestamp)handleMessageFailure
msg
(Object
): message property(sender, receiver, content, timestamp)handleGroupMessage
msg
(Object
): message property(sender, receiver, content, timestamp)handleGroupMessageACK
msg
(Object
): message property(sender, receiver, content, timestamp)handleGroupMessageFailure
msg
(Object
): message property(sender, receiver, content, timestamp)<script src="https://github.com/GoBelieveIO/im_js/raw/master/im.js"></script>
<script>
var observer = {
handlePeerMessage: function (msg) {
console.log("msg sender:", msg.sender, " receiver:", msg.receiver, " content:", msg.content, " timestamp:", msg.timestamp)
},
handleMessageACK: function(msg) {
console.log("message ack local id:", msgLocalID, " receiver:", receiver)
},
handleMessageFailure: function(msg) {
console.log("message fail local id:", msgLocalID, " receiver:", receiver)
},
onConnectState: function(state) {
if (state == IMService.STATE_CONNECTED) {
console.log("im connected");
} else if (state == IMService.STATE_CONNECTING) {
console.log("im connecting");
} else if (state == IMService.STATE_CONNECTFAIL) {
console.log("im connect fail");
} else if (state == IMService.STATE_UNCONNECTED) {
console.log("im unconnected");
}
},
handleGroupMessage: function(msg) {
console.log("group msg sender:", msg.sender, " receiver:", msg.receiver, " content:", msg.content, " timestamp:", msg.timestamp)
},
handleGroupMessageACK: function(msg) {
console.log("group message ack:", msg);
},
handleGroupMessageFailure: function(msg) {
console.log("group message fail:", msg);
},
handleGroupNotification(notification) {
var obj = JSON.parse(notification);
console.log("group notification:", obj);
},
}
var im = new IMService();
im.observer = observer;
im.accessToken = "????";
im.start()
var msg = {sender:100, receiver:200, content:"11", msgLocalID:1000}
//connectState == STATE_CONNECTING
var r = im.sendPeerMessage(msg);
//r == false
console.log("send message result:", r);
function send() {
var msg = {sender:100, receiver:100, content:"11", msgLocalID:1000}
if (im.connectState == IMService.STATE_CONNECTED) {
im.sendPeerMessage(msg);
}
}
setTimeout(send, 2000)
</script>