1+ // Example of how to use the event-based HyperfyAppServer
2+
3+ const { HyperfyAppServer, HyperfyAppServerHandler } = require ( './server.js' )
4+
5+ // Create a new server instance
6+ const server = new HyperfyAppServer ( 8080 , { hotReload : true } )
7+
8+ // Example 1: Add custom auth validation
9+ server . addMessageHandler ( 'auth' , ( ws , message ) => {
10+ console . log ( '🔐 Custom auth validation for user:' , message . userId )
11+
12+ // Example: Add custom validation logic
13+ if ( message . authToken && message . authToken . includes ( 'admin' ) ) {
14+ console . log ( '🔑 Admin user detected' )
15+ // You could add special privileges here
16+ }
17+ } )
18+
19+ // Example 2: Log all world snapshots
20+ server . addMessageHandler ( 'world_snapshot' , ( ws , message ) => {
21+ console . log ( '📸 World snapshot received:' , {
22+ worldUrl : message . worldUrl ,
23+ blueprintCount : message . blueprints . length ,
24+ entityCount : message . entities . length
25+ } )
26+ } )
27+
28+ // Example 3: Handle custom message types
29+ server . addMessageHandler ( 'custom_game_event' , ( ws , message ) => {
30+ console . log ( '🎮 Custom game event:' , message . eventType )
31+
32+ // Broadcast the event to all other worlds
33+ const worldUrl = server . getWorldUrlForSocket ( ws )
34+ server . broadcast ( {
35+ type : 'game_event_notification' ,
36+ fromWorld : worldUrl ,
37+ eventData : message
38+ } )
39+ } )
40+
41+ // Example 4: Monitor connections and disconnections
42+ server . addConnectionHandler ( ( ws , req ) => {
43+ console . log ( '👋 New client connected from:' , req . socket . remoteAddress )
44+ } )
45+
46+ server . addDisconnectHandler ( ( ws , userId ) => {
47+ if ( userId ) {
48+ console . log ( '🚪 User left:' , userId )
49+
50+ // Notify other users
51+ server . broadcast ( {
52+ type : 'user_left' ,
53+ userId : userId
54+ } )
55+ }
56+ } )
57+
58+ // Example 5: Handle unknown message types
59+ server . addMessageHandler ( 'message' , ( ws , message ) => {
60+ console . log ( '❓ Unknown message type received:' , message . type )
61+
62+ // You could log to analytics, handle special cases, etc.
63+ } )
64+
65+ // Example 6: Error handling
66+ server . on ( 'error' , ( error , ws , data ) => {
67+ console . error ( '💥 Message parsing error:' , error . message )
68+ // Could log to error tracking service
69+ } )
70+
71+ server . on ( 'websocket_error' , ( error , ws ) => {
72+ console . error ( '🔌 WebSocket error:' , error . message )
73+ // Could handle connection recovery
74+ } )
75+
76+ // Example 7: Periodic tasks using server state
77+ setInterval ( ( ) => {
78+ const connectedWorlds = server . getConnectedWorlds ( )
79+ console . log ( `📊 Server stats: ${ connectedWorlds . size } connected worlds` )
80+
81+ // Example: Send periodic updates to all worlds
82+ if ( connectedWorlds . size > 0 ) {
83+ server . broadcast ( {
84+ type : 'server_stats' ,
85+ timestamp : new Date ( ) . toISOString ( ) ,
86+ connectedUsers : connectedClients . size
87+ } )
88+ }
89+ } , 30000 ) // Every 30 seconds
90+
91+ // Example 8: Creating a custom handler class
92+ class CustomHyperfyHandler extends HyperfyAppServerHandler {
93+ constructor ( server ) {
94+ super ( server )
95+ }
96+
97+ // Override or extend default handlers
98+ handleAuth ( ws , message ) {
99+ console . log ( '🔐 Custom handler: Enhanced auth validation' )
100+
101+ // Call the parent handler for default behavior
102+ super . handleAuth ( ws , message )
103+
104+ // Add custom logic
105+ const { userId, authToken } = message
106+ if ( authToken && authToken . includes ( 'premium' ) ) {
107+ console . log ( '💎 Premium user detected:' , userId )
108+ // Add premium-specific logic
109+ }
110+ }
111+
112+ handleWorldSnapshot ( ws , message ) {
113+ console . log ( '📸 Custom handler: Enhanced world snapshot processing' )
114+
115+ // Call the parent handler
116+ super . handleWorldSnapshot ( ws , message )
117+
118+ // Add custom analytics or processing
119+ console . log ( '📊 Analytics: Recording world snapshot data' )
120+ }
121+ }
122+
123+ // Example 9: Using a custom handler instead of the default one
124+ function createServerWithCustomHandler ( ) {
125+ const customServer = new HyperfyAppServer ( 8081 , { hotReload : true } )
126+
127+ // Replace the default handler with our custom one
128+ const customHandler = new CustomHyperfyHandler ( customServer )
129+ customServer . defaultHandler = customHandler
130+ customHandler . attachHandlers ( )
131+
132+ return customServer
133+ }
134+
135+ // Start the server
136+ server . start ( )
137+
138+ console . log ( '🚀 Event-based Hyperfy server started with custom handlers!' )
139+ console . log ( '📖 See the code above for examples of:' )
140+ console . log ( ' - Adding custom message handlers' )
141+ console . log ( ' - Monitoring connections and disconnections' )
142+ console . log ( ' - Handling custom message types' )
143+ console . log ( ' - Error handling' )
144+ console . log ( ' - Creating custom handler classes' )
145+ console . log ( ' - Extending default behavior' )
0 commit comments