1
- const reconnectionAttempts = 3 ;
1
+ const MAX_CONNECTIONS_ATTEMPTS = 3 ;
2
2
const Addresses = [
3
3
`http://127.0.0.1:8787` ,
4
4
`https://syncify.giulioo.workers.dev` ,
@@ -7,6 +7,8 @@ const Addresses = [
7
7
let customButton ;
8
8
let syncify_observer ;
9
9
let socket ;
10
+ let disconnectedByUser = false ;
11
+ let attempts = 0 ;
10
12
11
13
function createButton ( ) {
12
14
const controlBar = document . querySelector ( '.main-nowPlayingBar-extraControls' ) ;
@@ -45,8 +47,10 @@ function createButton() {
45
47
customButton . addEventListener ( 'click' , ( ) => {
46
48
if ( customButton . classList . contains ( 'connected' ) ) {
47
49
showDialog ( "Disconnected" , "Successfully disconnected from Syncify servers!" )
50
+ disconnectedByUser = true ;
48
51
disconnect ( ) ;
49
52
} else {
53
+ disconnectedByUser = false ;
50
54
connect ( ) ;
51
55
}
52
56
} ) ;
@@ -93,22 +97,37 @@ async function attemptConnection(url, user_data) {
93
97
} ) ;
94
98
95
99
socket . send ( data ) ;
100
+ console . log ( "Attempting to connect to Syncify server..." )
96
101
} ) ;
97
102
98
103
socket . addEventListener ( "message" , ( event ) => {
99
104
const data = JSON . parse ( event . data ) ;
100
105
101
- if ( data . status == 'success ' ) {
106
+ if ( data . status == 'finalize ' ) {
102
107
window . open ( `${ url } /challenge?code=${ data . id } ` , '_blank' ) ;
108
+ console . log ( "Syncify server connection successful and finalization needed." )
109
+ resolve ( socket ) ;
110
+ }
111
+ else if ( data . status == 'success' ) {
112
+ console . log ( "Syncify server connection successful and finalization NOT needed." )
103
113
resolve ( socket ) ;
104
114
}
105
115
106
116
reject ( { 'type' : 'connection-error' , 'title' : "Syncify Server Connection Error" , 'message' : event . reason , 'fatal' : true } )
107
117
} ) ;
108
118
109
119
socket . addEventListener ( "close" , ( event ) => {
110
- console . log ( "disconnected" ) ;
111
- reject ( { 'type' : 'connection-error' , 'title' : "Syncify Server Connection Error" , 'message' : event . reason , 'fatal' : false } ) ;
120
+ disconnect ( ) ;
121
+ if ( ! disconnectedByUser && attempts < MAX_CONNECTIONS_ATTEMPTS ) {
122
+ showDialog ( "Connection Closed" , `Socket closed: ${ event . reason } , trying to establish a new connection...` ) ;
123
+ attempts += 1 ;
124
+ resolve ( attemptConnection ( url , user_data ) ) ;
125
+ }
126
+ else if ( ! disconnectedByUser ) {
127
+ showDialog ( "Connection Closed" , `Socket closed: ${ event . reason } , no more attempts left.` ) ;
128
+ reject ( { 'type' : 'connection-error' , 'title' : "Syncify Server Connection Error" , 'message' : event . reason , 'fatal' : true } ) ;
129
+ }
130
+ // reject({'type' : 'connection-error', 'title' : "Syncify Server Connection Error", 'message' : event.reason, 'fatal' : true});
112
131
} ) ;
113
132
} ) ;
114
133
} ;
@@ -128,17 +147,47 @@ async function findAvailableConnection() {
128
147
throw new Error ( 'Failed to connect to any selected Syncify Server. Try again later...' ) ;
129
148
} ;
130
149
150
+ async function searchSong ( socket , event , data ) {
151
+ console . log ( data ) ;
152
+ console . log ( `https://api.spotify.com/v1/search?q=${ data . query } &type=track,album,playlist,show,episode,audiobook&market=${ Spicetify . Platform . Session . locale } ` ) ;
153
+
154
+ const search_results = await Spicetify . CosmosAsync . get ( `https://api.spotify.com/v1/search?q=${ data . query } &type=track,album,playlist,show,episode,audiobook&limit=10&market=${ Spicetify . Platform . Session . locale } ` ) ;
155
+
156
+ const response = {
157
+ type : "search-results" ,
158
+ results : search_results ,
159
+ roomid : data . roomid ,
160
+ userid : data . userid
161
+ }
162
+
163
+ socket . send ( JSON . stringify ( response ) ) ;
164
+ }
165
+
131
166
async function connect ( ) {
132
- showDialog ( "Alomost there!" , `Just a moment while we try to connect you with Syncify Server ...` ) ;
167
+ showDialog ( "Alomost there!" , `Just a moment while we try to connect you with Syncify Servers ...` ) ;
133
168
setButtonStatus ( true ) ;
134
169
findAvailableConnection ( )
135
- . then ( ( socket ) => {
170
+ . then (
171
+ ( socket ) => {
172
+ socket . addEventListener ( "message" , async ( event ) => {
173
+ const data = JSON . parse ( event . data ) ;
174
+ switch ( data . type ) {
175
+ case 'search-song' :
176
+ await searchSong ( socket , event , data ) ;
177
+ break ;
178
+ }
179
+ } ) ;
180
+
136
181
// We need to implement the logic to handle the connection here
137
- } )
138
- . catch ( ( error ) => {
139
- showDialog ( 'Syncify Error' , error . message ) ;
140
- disconnect ( ) ;
141
- } )
182
+ } ,
183
+ ( reason ) => {
184
+ console . log ( reason ) ;
185
+ if ( reason . fatal ) showDialog ( reason . title , reason . message ) ;
186
+ } )
187
+ . catch ( ( error ) => {
188
+ showDialog ( 'Syncify Error' , error . message ) ;
189
+ disconnect ( ) ;
190
+ } )
142
191
} ;
143
192
144
193
function disconnect ( ) {
0 commit comments