创建节点
图对应代码
[
{
"id": "17969ffdb6285212",
"type": "tab",
"label": "流程 1",
"disabled": false,
"info": "",
"env": []
},
{
"id": "a4fa3965c4d7f20f",
"type": "debug",
"z": "17969ffdb6285212",
"name": "server",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 570,
"y": 340,
"wires": []
},
{
"id": "7cd897a932b7cd11",
"type": "tcp in",
"z": "17969ffdb6285212",
"name": "",
"server": "server",
"host": "",
"port": "1882",
"datamode": "stream",
"datatype": "utf8",
"newline": "",
"topic": "",
"base64": false,
"tls": "",
"x": 80,
"y": 400,
"wires": [
[
"99f3358137a547cc",
"1eafab2dda6a064c"
]
]
},
{
"id": "581c453726647ccb",
"type": "function",
"z": "17969ffdb6285212",
"name": "LOGIN",
"func": "var payload = msg.payload;\n\nif(payload.cmd == 100)//登录\n{\n // store the value back\n context.set(payload.imei, msg._session);//保存imei 对应id\n}\n\nif(payload.cmd == 101)//请求呼叫对方\n{\n var session = context.get(payload.to)||0;//根据对方imie查询session id\n if(session == 0)\n {\n return;\n }\n msg._session = session; //找到对方session id\n\n msg.payload = payload;//数据直接转发给对方\n}\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 380,
"y": 340,
"wires": [
[
"a4fa3965c4d7f20f",
"129ae15bee5723e7"
]
]
},
{
"id": "99f3358137a547cc",
"type": "debug",
"z": "17969ffdb6285212",
"name": "client",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "_session",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 250,
"y": 600,
"wires": []
},
{
"id": "1eafab2dda6a064c",
"type": "json",
"z": "17969ffdb6285212",
"name": "",
"property": "payload",
"action": "",
"pretty": false,
"x": 230,
"y": 340,
"wires": [
[
"581c453726647ccb",
"28c2a4ef6d68c9a8"
]
]
},
{
"id": "28c2a4ef6d68c9a8",
"type": "debug",
"z": "17969ffdb6285212",
"name": "js",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 370,
"y": 260,
"wires": []
},
{
"id": "129ae15bee5723e7",
"type": "json",
"z": "17969ffdb6285212",
"name": "",
"property": "payload",
"action": "",
"pretty": false,
"x": 370,
"y": 420,
"wires": [
[
"c28bb1f9386c906c"
]
]
},
{
"id": "c28bb1f9386c906c",
"type": "tcp out",
"z": "17969ffdb6285212",
"name": "",
"host": "",
"port": "",
"beserver": "reply",
"base64": false,
"end": false,
"tls": "",
"x": 370,
"y": 500,
"wires": []
}
]
函数处理
var payload = msg.payload;
if(payload.cmd == 100)
{
context.set(payload.imei, msg._session);
}
if(payload.cmd == 101)
{
var session = context.get(payload.to)||0;
if(session == 0)
{
return;
}
msg._session = session;
msg.payload = payload;
}
return msg;
测试
A发送指令登录
{"cmd":100,"imei":"862838050007731"}
B发送指令登录
{"cmd":100,"imei":"860311031079866"}
B发送指令请求通话
{"cmd":101,"from":"860311031079866","to":"862838050007731"}
A接收到请求通话的指令