[
{
"id": "ws_sample_tab",
"type": "tab",
"label": "WebSocket サンプル",
"disabled": false,
"info": ""
},
{
"id": "ws_listener_echo",
"type": "websocket-listener",
"path": "/ws/echo",
"wholemsg": "false"
},
{
"id": "ws_listener_chat",
"type": "websocket-listener",
"path": "/ws/chat",
"wholemsg": "false"
},
{
"id": "ws_comment1",
"type": "comment",
"z": "ws_sample_tab",
"name": "━━━ エコーサーバー(受信→返信) ━━━",
"info": "ws://localhost:1880/ws/echo",
"x": 220,
"y": 40,
"wires": []
},
{
"id": "ws_in_echo",
"type": "websocket in",
"z": "ws_sample_tab",
"name": "WS Echo In",
"server": "ws_listener_echo",
"client": "",
"x": 150,
"y": 100,
"wires": [["ws_func_echo", "ws_debug1"]]
},
{
"id": "ws_func_echo",
"type": "function",
"z": "ws_sample_tab",
"name": "エコー処理",
"func": "// 受信データに時刻を追加して返信\nvar received = msg.payload;\n\nmsg.payload = {\n type: \"echo\",\n original: received,\n timestamp: new Date().toISOString(),\n message: \"あなたは「\" + received + \"」と言いました\"\n};\n\n// JSON文字列に変換\nmsg.payload = JSON.stringify(msg.payload);\n\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 350,
"y": 100,
"wires": [["ws_out_echo"]]
},
{
"id": "ws_out_echo",
"type": "websocket out",
"z": "ws_sample_tab",
"name": "WS Echo Out",
"server": "ws_listener_echo",
"client": "",
"x": 550,
"y": 100,
"wires": []
},
{
"id": "ws_debug1",
"type": "debug",
"z": "ws_sample_tab",
"name": "受信ログ",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": true,
"complete": "payload",
"targetType": "msg",
"statusVal": "\"受信\"",
"statusType": "str",
"x": 350,
"y": 140,
"wires": []
},
{
"id": "ws_comment2",
"type": "comment",
"z": "ws_sample_tab",
"name": "━━━ ブロードキャスト(全員に配信) ━━━",
"info": "ws://localhost:1880/ws/chat",
"x": 220,
"y": 220,
"wires": []
},
{
"id": "ws_in_chat",
"type": "websocket in",
"z": "ws_sample_tab",
"name": "WS Chat In",
"server": "ws_listener_chat",
"client": "",
"x": 150,
"y": 280,
"wires": [["ws_func_chat", "ws_debug2"]]
},
{
"id": "ws_func_chat",
"type": "function",
"z": "ws_sample_tab",
"name": "チャット処理",
"func": "// 送信者のセッションIDを保存\nvar senderSession = msg._session;\n\ntry {\n var data = JSON.parse(msg.payload);\n} catch(e) {\n var data = {name: \"匿名\", message: msg.payload};\n}\n\n// ブロードキャスト用にメッセージを整形\nmsg.payload = JSON.stringify({\n type: \"chat\",\n name: data.name || \"匿名\",\n message: data.message || data,\n timestamp: new Date().toISOString()\n});\n\n// _sessionを削除して全員に送信\ndelete msg._session;\n\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 350,
"y": 280,
"wires": [["ws_out_chat"]]
},
{
"id": "ws_out_chat",
"type": "websocket out",
"z": "ws_sample_tab",
"name": "WS Chat Out (全員)",
"server": "ws_listener_chat",
"client": "",
"x": 580,
"y": 280,
"wires": []
},
{
"id": "ws_debug2",
"type": "debug",
"z": "ws_sample_tab",
"name": "チャットログ",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": true,
"complete": "payload",
"targetType": "msg",
"statusVal": "\"チャット受信\"",
"statusType": "str",
"x": 370,
"y": 320,
"wires": []
},
{
"id": "ws_comment3",
"type": "comment",
"z": "ws_sample_tab",
"name": "━━━ サーバーからのプッシュ配信 ━━━",
"info": "",
"x": 210,
"y": 400,
"wires": []
},
{
"id": "ws_inject_push",
"type": "inject",
"z": "ws_sample_tab",
"name": "プッシュ送信",
"props": [{"p": "payload"}],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "{\"type\":\"notification\",\"message\":\"サーバーからのお知らせです\"}",
"payloadType": "json",
"x": 160,
"y": 460,
"wires": [["ws_func_push"]]
},
{
"id": "ws_inject_timer",
"type": "inject",
"z": "ws_sample_tab",
"name": "5秒ごとに時刻配信",
"props": [{"p": "payload"}],
"repeat": "5",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 180,
"y": 520,
"wires": [["ws_func_time"]]
},
{
"id": "ws_func_push",
"type": "function",
"z": "ws_sample_tab",
"name": "プッシュ整形",
"func": "msg.payload = JSON.stringify(msg.payload);\n// _sessionがないので全員に送信\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 370,
"y": 460,
"wires": [["ws_out_chat"]]
},
{
"id": "ws_func_time",
"type": "function",
"z": "ws_sample_tab",
"name": "時刻整形",
"func": "msg.payload = JSON.stringify({\n type: \"time\",\n serverTime: new Date().toISOString()\n});\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 360,
"y": 520,
"wires": [["ws_out_chat"]]
},
{
"id": "ws_comment4",
"type": "comment",
"z": "ws_sample_tab",
"name": "━━━ ブラウザテスト用HTMLページ ━━━",
"info": "http://localhost:1880/ws-test",
"x": 220,
"y": 600,
"wires": []
},
{
"id": "ws_http_in_test",
"type": "http in",
"z": "ws_sample_tab",
"name": "GET /ws-test",
"url": "/ws-test",
"method": "get",
"upload": false,
"swaggerDoc": "",
"x": 160,
"y": 660,
"wires": [["ws_template_test"]]
},
{
"id": "ws_template_test",
"type": "template",
"z": "ws_sample_tab",
"name": "テストページHTML",
"field": "payload",
"fieldType": "msg",
"format": "html",
"syntax": "plain",
"template": "\n\n\n
\n
WebSocket テスト\n \n\n\n
\n
🔌 WebSocket テスト
\n \n
切断中
\n \n
\n \n \n \n
\n \n
\n \n
\n \n \n
\n
\n \n \n\n",
"output": "str",
"x": 390,
"y": 660,
"wires": [["ws_http_response_test"]]
},
{
"id": "ws_http_response_test",
"type": "http response",
"z": "ws_sample_tab",
"name": "Response",
"statusCode": "200",
"headers": {"Content-Type": "text/html; charset=utf-8"},
"x": 600,
"y": 660,
"wires": []
}
]