@@ -3,6 +3,7 @@ import * as vscode from "vscode"
3
3
import { WebSocket } from "ws"
4
4
import { errToStr } from "./api-helper"
5
5
import { Storage } from "./storage"
6
+ import { ProxyAgent } from "proxy-agent"
6
7
7
8
type InboxMessage = {
8
9
unread_count : number
@@ -29,31 +30,33 @@ export class Inbox implements vscode.Disposable {
29
30
private socket : WebSocket
30
31
31
32
constructor (
32
- private readonly restClient : Api ,
33
+ httpAgent : ProxyAgent ,
34
+ restClient : Api ,
33
35
private readonly storage : Storage ,
34
36
) {
35
- // const url = this.restClient.getAxiosInstance().defaults.baseURL
36
- const token = this . restClient . getAxiosInstance ( ) . defaults . headers . common [ "Coder-Session-Token" ] as
37
- | string
38
- | undefined
39
- // const inboxUrl = new URL(`${url}/api/v2/notifications/watch`);
40
- const inboxUrl = new URL ( `ws://localhost:8080` )
41
-
42
- this . storage . writeToCoderOutputChannel ( "Listening to Coder Inbox" )
43
-
44
- // We're gonna connect over WebSocket so replace the scheme.
45
- if ( inboxUrl . protocol === "https" ) {
46
- inboxUrl . protocol = "wss"
47
- } else if ( inboxUrl . protocol === "http" ) {
48
- inboxUrl . protocol = "ws"
37
+ const baseUrlRaw = restClient . getAxiosInstance ( ) . defaults . baseURL
38
+ if ( ! baseUrlRaw ) {
39
+ throw new Error ( "No base URL set on REST client" )
49
40
}
50
41
51
- this . socket = new WebSocket ( inboxUrl , {
42
+ const baseUrl = new URL ( baseUrlRaw )
43
+ const socketProto = baseUrl . protocol === "https:" ? "wss:" : "ws:"
44
+ const socketUrlRaw = `${ socketProto } //${ baseUrl . host } /api/v2/notifications/watch`
45
+
46
+ this . socket = new WebSocket ( new URL ( socketUrlRaw ) , {
47
+ followRedirects : true ,
48
+ agent : httpAgent ,
52
49
headers : {
53
- "Coder-Session-Token" : token ,
50
+ "Coder-Session-Token" : restClient . getAxiosInstance ( ) . defaults . headers . common [ "Coder-Session-Token" ] as
51
+ | string
52
+ | undefined ,
54
53
} ,
55
54
} )
56
55
56
+ this . socket . on ( "open" , ( ) => {
57
+ this . storage . writeToCoderOutputChannel ( "Listening to Coder Inbox" )
58
+ } )
59
+
57
60
this . socket . on ( "error" , ( error ) => {
58
61
this . notifyError ( error )
59
62
} )
0 commit comments