1- import { serve } from "@hono/node-server" ;
21import { Hono } from "hono" ;
32import { cors } from "hono/cors" ;
43import { createNodeWebSocket } from "@hono/node-ws" ;
54import { handler } from "./handler" ;
65import { config } from "dotenv" ;
76import router from "./api" ;
87import { authClient , tokenToSession } from "./lib/auth-client" ;
8+ import { createServer } from "http" ;
9+ import { MessageType , pack } from "@kbnet/shared" ;
10+ import { serve } from "@hono/node-server" ;
911
1012config ( ) ;
1113
@@ -28,7 +30,7 @@ app.get("/", (c) => {
2830 } ) ;
2931} ) ;
3032
31- app . use ( "*" , async ( c , next ) => {
33+ app . use ( "/api/ *" , async ( c , next ) => {
3234 if ( c . req . path . startsWith ( "/ws" ) ) {
3335 return next ( ) ;
3436 }
@@ -52,13 +54,6 @@ app.use("*", async (c, next) => {
5254 }
5355} ) ;
5456
55- app . use (
56- "/api/*" ,
57- cors ( {
58- origin : "*" , // Allow all origins for API routes
59- } )
60- ) ;
61-
6257app . route ( "/api" , router ) ;
6358
6459app . get (
@@ -69,9 +64,11 @@ app.get(
6964 onOpen : async ( evt , ws ) => {
7065 const token = c . req . query ( "token" ) ;
7166 try {
72- await tokenToSession ( c , token ) ;
67+ const { session, user } = await tokenToSession ( c , token ) ;
68+
69+ c . set ( "user" , user ) ;
70+ c . set ( "session" , session ) ;
7371 } catch ( error : any ) {
74- console . error ( "WebSocket authentication failed:" , error ) ;
7572 ws . send (
7673 JSON . stringify ( {
7774 type : "error" ,
@@ -83,11 +80,13 @@ app.get(
8380 return ;
8481 }
8582 console . log ( "WebSocket connection opened" , evt . type ) ;
86- ws . send ( "Welcome to KBNet WebSocket!" ) ;
83+ ws . send (
84+ pack ( MessageType . WELCOME , { message : "Welcome to KBNet WebSocket!" } )
85+ ) ;
8786
8887 // Send ping every 30 seconds
8988 pingInterval = setInterval ( ( ) => {
90- if ( ws . readyState === WebSocket . OPEN ) {
89+ if ( ws . readyState === 1 ) {
9190 ws . send ( JSON . stringify ( { type : "ping" } ) ) ;
9291 }
9392 } , 30000 ) ;
@@ -100,12 +99,6 @@ app.get(
10099 handler . cleanUpSocket ( ws ) ;
101100 } ,
102101 onMessage : ( evt , ws ) => {
103- const session = c . get ( "session" ) ;
104- const user = c . get ( "user" ) ;
105- if ( ! session || ! user ) {
106- console . warn ( "Session or user not found on message" ) ;
107- return ;
108- }
109102 handler . handle ( c , evt , ws ) ;
110103 } ,
111104 onError ( evt , ws ) {
@@ -118,15 +111,23 @@ app.get(
118111) ;
119112
120113const PORT = parseInt ( process . env . PORT || "8000" ) ;
114+
115+ // const server = createServer(app);
116+
121117const server = serve (
122118 {
123119 fetch : app . fetch ,
124120 port : PORT ,
125121 hostname : "0.0.0.0" ,
122+ createServer,
126123 } ,
127124 ( info ) => {
128125 console . log ( `Server is running on http://localhost:${ info . port } ` ) ;
129126 }
130127) ;
131128
132129injectWebSocket ( server ) ;
130+
131+ // server.listen(PORT, "0.0.0.0", () => {
132+ // console.log(`Server running at http://0.0.0.0:${PORT}/`);
133+ // });
0 commit comments