soiz1 commited on
Commit
7c730f1
·
1 Parent(s): dac35bc

Update web-socket.html

Browse files
Files changed (1) hide show
  1. web-socket.html +108 -104
web-socket.html CHANGED
@@ -63,115 +63,119 @@
63
  </table>
64
 
65
  <script>
66
- const socket = io("https://web-socket-server-14ap.onrender.com/", {
67
- query: {
68
- isAdmin: true // 管理画面からの接続であることを示す
69
- }
70
- });
71
-
72
- const STORAGE_KEY = "userSessions";
73
-
74
- let previousSessions = JSON.parse(localStorage.getItem(STORAGE_KEY) || "{}");
75
-
76
- const userCountElem = document.getElementById("userCount");
77
- const tbody = document.getElementById("userTableBody");
78
-
79
- const map = L.map('map').setView([35.681236, 139.767125], 5);
80
- L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
81
- maxZoom: 18,
82
- attribution: '© OpenStreetMap contributors'
83
- }).addTo(map);
84
-
85
- let markers = {};
86
-
87
- function createCircleMarker(lat, lon, color, popupText) {
88
- return L.circleMarker([lat, lon], {
89
- radius: 10,
90
- fillColor: color,
91
- color: '#000',
92
- weight: 1,
93
- opacity: 1,
94
- fillOpacity: 0.9
95
- }).bindPopup(popupText);
96
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
 
98
- socket.on("updateUserCount", (count) => {
99
- userCountElem.textContent = count;
100
- });
101
-
102
- socket.on("userSessionsUpdate", (userSessions) => {
103
- console.log("userSessionsUpdate受信", userSessions);
104
-
105
- tbody.innerHTML = "";
106
-
107
- for (let key in markers) {
108
- map.removeLayer(markers[key]);
109
- }
110
- markers = {};
111
-
112
- for (const [socketId, info] of Object.entries(userSessions)) {
113
- const tr = document.createElement("tr");
114
-
115
- function createTd(text, elementKey) {
116
- const td = document.createElement("td");
117
- td.textContent = text;
118
-
119
- const prevValue = previousSessions[socketId] ? previousSessions[socketId][elementKey] : undefined;
120
- const currValue = info[elementKey];
121
-
122
- const changed = prevValue !== currValue;
123
-
124
- if (changed) {
125
- td.classList.add("highlight");
126
- setTimeout(() => td.classList.remove("highlight"), 1000);
127
- }
128
-
129
- return td;
130
- }
131
-
132
- tr.appendChild(createTd(socketId, "socketId"));
133
- tr.appendChild(createTd(info.ip || "-", "ip"));
134
- tr.appendChild(createTd(info.connectTime ? new Date(info.connectTime).toLocaleString() : "-", "connectTime"));
135
- tr.appendChild(createTd(info.disconnectTime ? new Date(info.disconnectTime).toLocaleString() : "-", "disconnectTime"));
136
- tr.appendChild(createTd(info.loc || "-", "loc"));
137
- tr.appendChild(createTd(info.city || "-", "city"));
138
- tr.appendChild(createTd(info.region || "-", "region"));
139
- tr.appendChild(createTd(info.country || "-", "country"));
140
-
141
- tbody.appendChild(tr);
142
-
143
- // 位置情報がある場合、地図にマーカーを追加
144
- if (info.loc) {
145
- const [lat, lon] = info.loc.split(',').map(Number);
146
- const prevLoc = previousSessions[socketId]?.loc;
147
- const changedPos = prevLoc !== info.loc;
148
- const color = changedPos ? 'red' : 'blue';
149
-
150
- const locationText = [
151
- `SocketID: ${socketId}`,
152
- `IP: ${info.ip || "-"}`,
153
- `都市: ${info.city || "-"}`,
154
- `地域: ${info.region || "-"}`,
155
- `国: ${info.country || "-"}`
156
- ].join('<br>');
157
-
158
- const marker = createCircleMarker(lat, lon, color, locationText);
159
- marker.addTo(map);
160
- markers[socketId] = marker;
161
- }
162
- }
163
 
164
- // マーカーがある場合、地図を適切な範囲に調整
165
- if (Object.keys(markers).length > 0) {
166
- const group = L.featureGroup(Object.values(markers));
167
- map.fitBounds(group.getBounds().pad(0.5));
168
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169
 
170
- // セッション情報をローカルストレージに保存
171
- localStorage.setItem(STORAGE_KEY, JSON.stringify(userSessions));
172
- previousSessions = userSessions;
173
- });
 
174
 
 
 
 
175
  </script>
176
  </body>
177
  </html>
 
63
  </table>
64
 
65
  <script>
66
+ const socket = io("https://web-socket-server-14ap.onrender.com/", {
67
+ query: {
68
+ isAdmin: true // 管理画面からの接続であることを示す
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  }
70
+ });
71
+
72
+ const STORAGE_KEY = "userSessions";
73
+
74
+ // 過去・現在含む全セッション情報を管理
75
+ let allSessions = JSON.parse(localStorage.getItem(STORAGE_KEY) || "{}");
76
+
77
+ const userCountElem = document.getElementById("userCount");
78
+ const tbody = document.getElementById("userTableBody");
79
+
80
+ const map = L.map('map').setView([35.681236, 139.767125], 5);
81
+ L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
82
+ maxZoom: 18,
83
+ attribution: '© OpenStreetMap contributors'
84
+ }).addTo(map);
85
+
86
+ let markers = {};
87
+
88
+ function createCircleMarker(lat, lon, color, popupText) {
89
+ return L.circleMarker([lat, lon], {
90
+ radius: 10,
91
+ fillColor: color,
92
+ color: '#000',
93
+ weight: 1,
94
+ opacity: 1,
95
+ fillOpacity: 0.9
96
+ }).bindPopup(popupText);
97
+ }
98
+
99
+ // ユーザー数更新
100
+ socket.on("updateUserCount", (count) => {
101
+ userCountElem.textContent = count;
102
+ });
103
+
104
+ // 現在接続中のセッション更新(リアルタイム)
105
+ socket.on("userSessionsUpdate", (userSessions) => {
106
+ // 現在のセッションをallSessionsにマージ(上書き)
107
+ for (const [socketId, info] of Object.entries(userSessions)) {
108
+ allSessions[socketId] = info;
109
+ }
110
+ renderTable(allSessions);
111
+ });
112
+
113
+ // 過去セッション履歴受信時
114
+ socket.on("userSessionHistory", (sessionHistory) => {
115
+ // 履歴の各セッションをallSessionsに追加(既存のsocketIdを上書きしない)
116
+ for (const session of sessionHistory) {
117
+ if (!allSessions[session.socketId]) {
118
+ allSessions[session.socketId] = session;
119
+ }
120
+ }
121
+ renderTable(allSessions);
122
+ });
123
 
124
+ function renderTable(sessions) {
125
+ tbody.innerHTML = "";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
 
127
+ // 既存マーカー削除
128
+ for (let key in markers) {
129
+ map.removeLayer(markers[key]);
130
+ }
131
+ markers = {};
132
+
133
+ for (const [socketId, info] of Object.entries(sessions)) {
134
+ const tr = document.createElement("tr");
135
+
136
+ function createTd(text) {
137
+ const td = document.createElement("td");
138
+ td.textContent = text;
139
+ return td;
140
+ }
141
+
142
+ tr.appendChild(createTd(socketId));
143
+ tr.appendChild(createTd(info.ip || "-"));
144
+ tr.appendChild(createTd(info.connectTime ? new Date(info.connectTime).toLocaleString() : "-"));
145
+ tr.appendChild(createTd(info.disconnectTime ? new Date(info.disconnectTime).toLocaleString() : "-"));
146
+ tr.appendChild(createTd(info.loc || "-"));
147
+ tr.appendChild(createTd(info.city || "-"));
148
+ tr.appendChild(createTd(info.region || "-"));
149
+ tr.appendChild(createTd(info.country || "-"));
150
+
151
+ tbody.appendChild(tr);
152
+
153
+ // 位置情報がある場合は地図にマーカーを追加
154
+ if (info.loc) {
155
+ const [lat, lon] = info.loc.split(',').map(Number);
156
+ const locationText = [
157
+ `SocketID: ${socketId}`,
158
+ `IP: ${info.ip || "-"}`,
159
+ `都市: ${info.city || "-"}`,
160
+ `地域: ${info.region || "-"}`,
161
+ `国: ${info.country || "-"}`
162
+ ].join('<br>');
163
+
164
+ const marker = createCircleMarker(lat, lon, 'blue', locationText);
165
+ marker.addTo(map);
166
+ markers[socketId] = marker;
167
+ }
168
+ }
169
 
170
+ // マーカーがあれば地図を調整
171
+ if (Object.keys(markers).length > 0) {
172
+ const group = L.featureGroup(Object.values(markers));
173
+ map.fitBounds(group.getBounds().pad(0.5));
174
+ }
175
 
176
+ // ローカルストレージに保存
177
+ localStorage.setItem(STORAGE_KEY, JSON.stringify(sessions));
178
+ }
179
  </script>
180
  </body>
181
  </html>