融云服务端实时消息路由Demo

前言

使用融云服务端实时消息路由功能需要开通融云的高级功能,该功能主要是将消息实时保存到本地的数据库,具体参考文档

Demo

要实现实时消息路由需要自己实现一个web服务器,这里以Django为例。创建完Django项目、App后,编写Model层,再编写Veiw层如下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from .models import Message
import json
import time


@csrf_exempt #Django CSRF处理
def sync(request):
if request.method == 'POST':
fromUserId = request.POST.get('fromUserId')
toUserId = request.POST.get('toUserId')
objectName = request.POST.get('objectName')
channelType = request.POST.get('channelType')
msgTimestamp = request.POST.get('msgTimestamp')
msgUID = request.POST.get('msgUID')
sensitiveType = request.POST.get('sensitiveType')
source = request.POST.get('source')
groupUserIds = request.POST.get('groupUserIds')

# 类型处理
object = objectName.split(':')
rc = object[1]

# 内容处理
global content
if rc == 'TxtMsg':
body = request.POST.get('content')
body = eval(body)
content = body.get('content')
elif rc == 'ImgMsg':
body = request.POST.get('content')
body = json.loads(body)
content = body.get('imageUri')
elif rc == 'VcMsg':
content = 'VcMsg'
elif rc == 'FileMsg':
content = 'FileMsg'
else:
content = 'otherMsg'

# 时间处理
t = time.localtime(float(msgTimestamp[:10]))
msgTime = time.strftime('%Y-%m-%d %H:%M:%S', t)

message = Message(fromUserId=fromUserId,
toUserId=toUserId,
objectName=rc,
content=content,
channelType=channelType,
msgTime=msgTime,
msgUID=msgUID,
sensitiveType=sensitiveType,
source=source,
groupUserIds=groupUserIds)
message.save()

return HttpResponse(status=200)
return HttpResponse('OK', status=200)

融云需要你响应status=200就行了。然后设置消息路由地址,它会先给你发个测试数据,然后2小时内生效。

----------本文完,感谢您的阅读----------