-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
71 lines (57 loc) · 2.37 KB
/
server.ts
File metadata and controls
71 lines (57 loc) · 2.37 KB
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
60
61
62
63
64
65
66
67
68
69
/**
* カスタムNode.jsサーバー
* Next.js 16.1 + Socket.ioを統合し、リアルタイムエージェント通信を実現
* HMR (Hot Module Replacement) との互換性を維持
*/
import { createServer } from 'node:http';
import next from 'next';
import { Server as SocketIOServer } from 'socket.io';
const dev = process.env.NODE_ENV !== 'production';
const hostname = process.env.HOSTNAME || '0.0.0.0';
const port = Number.parseInt(process.env.PORT || '3000', 10);
console.log('Server configuration:', { dev, hostname, port, env: process.env.NODE_ENV });
// Next.jsアプリケーションの初期化
const nextApp = next({ dev, hostname, port });
const handler = nextApp.getRequestHandler();
nextApp.prepare().then(() => {
// HTTPサーバーを作成
const httpServer = createServer((req, res) => {
handler(req, res);
});
// Socket.ioサーバーをHTTPサーバーに統合
const io = new SocketIOServer(httpServer, {
cors: {
origin: dev ? 'http://localhost:3000' : process.env.ALLOWED_ORIGIN || '*',
methods: ['GET', 'POST'],
},
});
// Socket.io接続ハンドラー
io.on('connection', (socket) => {
console.log(`[Socket.io] クライアント接続: ${socket.id}`);
// エージェント位置更新
socket.on('agent:position', (data: { agentId: string; lat: number; lng: number }) => {
// 他の全クライアントにブロードキャスト
socket.broadcast.emit('agent:position:update', data);
});
// エージェント状態変更 (Idle, Negotiating, Moving)
socket.on('agent:state', (data: { agentId: string; state: string }) => {
socket.broadcast.emit('agent:state:update', data);
});
// ネゴシエーションメッセージ
socket.on('agent:message', (data: { from: string; to: string; message: string }) => {
socket.broadcast.emit('agent:message:received', data);
});
socket.on('disconnect', () => {
console.log(`[Socket.io] クライアント切断: ${socket.id}`);
});
});
// サーバー起動
httpServer.listen(port, hostname, () => {
console.log(`🚀 サーバー起動: http://${hostname}:${port}`);
console.log(`⚡ 環境: ${dev ? '開発モード (HMR有効)' : '本番モード'}`);
console.log(`🔌 Socket.io 準備完了`);
});
}).catch((err) => {
console.error('Server initialization failed:', err);
process.exit(1);
});