1+ import  {  randomPort  }  from  "../../common/src" ; 
2+ import  {  SCTP ,  WEBRTC_PPID  }  from  "../src" ; 
3+ import  {  createUdpTransport  }  from  "../src/transport" ; 
4+ import  {  createSocket  }  from  "node:dgram" ; 
5+ 
6+ async  function  main ( )  { 
7+   console . log ( "SCTP Session Migration Example with UDP Transport" ) ; 
8+   
9+   // Get random ports for UDP communication 
10+   const  clientPort  =  await  randomPort ( ) ; 
11+   const  serverPort  =  await  randomPort ( ) ; 
12+   
13+   console . log ( `Client port: ${ clientPort } ${ serverPort }  ) ; 
14+   
15+   // Create SCTP sessions with UDP transport 
16+   const  sctpA  =  SCTP . client ( 
17+     createUdpTransport ( createSocket ( "udp4" ) . bind ( clientPort ) ,  { 
18+       port : serverPort , 
19+       address : "127.0.0.1" , 
20+     } ) , 
21+   ) ; 
22+   const  sctpB  =  SCTP . server ( 
23+     createUdpTransport ( createSocket ( "udp4" ) . bind ( serverPort ) ,  { 
24+       port : clientPort , 
25+       address : "127.0.0.1" , 
26+     } ) , 
27+   ) ; 
28+   
29+   // Set up event listeners 
30+   sctpB . onReceive . subscribe ( ( streamId ,  ppId ,  data )  =>  { 
31+     console . log ( `Received from stream ${ streamId } ${ data . toString ( ) }  ) ; 
32+   } ) ; 
33+   
34+   // Start sessions and wait for connection 
35+   console . log ( "Starting SCTP sessions..." ) ; 
36+   await  Promise . all ( [ sctpA . start ( ) ,  sctpB . start ( ) ] ) ; 
37+   
38+   // Wait for connection to be established 
39+   await  new  Promise < void > ( ( resolve ,  reject )  =>  { 
40+     const  timeout  =  setTimeout ( ( )  =>  reject ( new  Error ( "Connection timeout" ) ) ,  5000 ) ; 
41+     const  checkConnection  =  ( )  =>  { 
42+       if  ( sctpA . state  ===  "connected"  &&  sctpB . state  ===  "connected" )  { 
43+         clearTimeout ( timeout ) ; 
44+         resolve ( ) ; 
45+       }  else  { 
46+         setTimeout ( checkConnection ,  10 ) ; 
47+       } 
48+     } ; 
49+     checkConnection ( ) ; 
50+   } ) ; 
51+   
52+   console . log ( "Connection established" ) ; 
53+   
54+   // Send some data from session A 
55+   await  sctpA . send ( 0 ,  WEBRTC_PPID . STRING ,  Buffer . from ( "Hello from session A" ) ) ; 
56+   console . log ( "Sent data from session A" ) ; 
57+   
58+   // Send data on multiple streams 
59+   await  sctpA . send ( 1 ,  WEBRTC_PPID . STRING ,  Buffer . from ( "Stream 1 data" ) ) ; 
60+   console . log ( "Sent data on stream 1" ) ; 
61+   
62+   // Wait a bit for data to be processed 
63+   await  new  Promise ( resolve  =>  setTimeout ( resolve ,  200 ) ) ; 
64+   
65+   // Export session A state 
66+   console . log ( "Exporting session A state..." ) ; 
67+   const  stateBuffer  =  sctpA . exportState ( ) ; 
68+   console . log ( `Exported state size: ${ stateBuffer . length }  ) ; 
69+   
70+   // Create new transport for restored session with a new port 
71+   const  newClientPort  =  await  randomPort ( ) ; 
72+   console . log ( `New client port for migration: ${ newClientPort }  ) ; 
73+   
74+   const  transportA2  =  createUdpTransport ( createSocket ( "udp4" ) . bind ( newClientPort ) ,  { 
75+     port : serverPort , 
76+     address : "127.0.0.1" , 
77+   } ) ; 
78+   
79+   // Restore session from exported state 
80+   console . log ( "Restoring session from exported state..." ) ; 
81+   const  sctpA2  =  SCTP . restoreState ( stateBuffer ,  transportA2 ) ; 
82+   
83+   // Update server's remote port to communicate with the new client 
84+   sctpB . setRemotePort ( newClientPort ) ; 
85+   
86+   console . log ( "Session migrated successfully" ) ; 
87+   console . log ( `Original session state: ${ sctpA . state }  ) ; 
88+   console . log ( `Restored session state: ${ sctpA2 . state }  ) ; 
89+   console . log ( `Restored session is server: ${ sctpA2 . isServer }  ) ; 
90+   console . log ( `Restored session port: ${ sctpA2 . port }  ) ; 
91+   console . log ( `Restored session max channels: ${ sctpA2 . maxChannels }  ) ; 
92+   
93+   // Stop original session 
94+   await  sctpA . stop ( ) ; 
95+   console . log ( "Original session stopped" ) ; 
96+   
97+   // Send data from restored session 
98+   await  sctpA2 . send ( 0 ,  WEBRTC_PPID . STRING ,  Buffer . from ( "Hello from migrated session A" ) ) ; 
99+   console . log ( "Sent data from migrated session on stream 0" ) ; 
100+   
101+   await  sctpA2 . send ( 1 ,  WEBRTC_PPID . STRING ,  Buffer . from ( "Migrated stream 1 data" ) ) ; 
102+   console . log ( "Sent data from migrated session on stream 1" ) ; 
103+   
104+   // Wait for messages to be received 
105+   await  new  Promise ( resolve  =>  setTimeout ( resolve ,  200 ) ) ; 
106+   
107+   // Clean up 
108+   await  sctpA2 . stop ( ) ; 
109+   await  sctpB . stop ( ) ; 
110+   
111+   console . log ( "Session migration example completed successfully" ) ; 
112+ } 
113+ 
114+ if  ( require . main  ===  module )  { 
115+   main ( ) . catch ( console . error ) ; 
116+ } 
0 commit comments