Skip to content

Commit 565eb83

Browse files
committed
some better fail cases if voicecode isn’t reachable
1 parent 60c1830 commit 565eb83

2 files changed

Lines changed: 19 additions & 10 deletions

File tree

SourceEditorExtension/VoiceCodeSourceEditorCommand.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class SourceEditorCommand: NSObject, XCSourceEditorCommand {
9595

9696
service.getLatestCommand() { (command) in
9797
//NSLog(buffer.contentUTI)
98-
NSLog(command)
98+
NSLog("Command received: " + command)
9999

100100
do {
101101
let json = try JSON(data: command.data(using: .utf8)!)
@@ -116,9 +116,11 @@ class SourceEditorCommand: NSObject, XCSourceEditorCommand {
116116
let line = clampLineNumber(lineNumber: json["line"].intValue - 1, nLinesInBuffer: nLinesInBuffer)
117117

118118
switch(json["id"].stringValue) {
119-
119+
case "no message":
120+
NSLog("Could not connect to VoiceCode websocket")
121+
break
120122
case "initial-state":
121-
NSLog("initial state - not handled")
123+
NSLog("Initial state - not handled")
122124
break
123125

124126
// MARK: -

XPCService/VoiceCodeXPCService.swift

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,48 @@ import Starscream
66

77
var socket: WebSocket
88
var messageReceived: String
9+
var failedToConnect: Bool
10+
let noMessageStr = "{\"id\": \"no message\"}"
911

1012
override init() {
1113
socket = WebSocket(url: URL(string: "ws://localhost:8081/")!)
12-
messageReceived = ""
14+
messageReceived = noMessageStr
15+
failedToConnect = false
1316
super.init()
1417
socket.delegate = self
1518
socket.connect()
1619
}
1720

1821
func getLatestCommand(withReply: (String) -> ()) {
1922

20-
while(!socket.isConnected) {
23+
//we need to handle this better here - it will block until the connection is made, if it can't make the connection
24+
while(!socket.isConnected && !failedToConnect) {
2125
// NSLog("connecting")
2226
}
2327

24-
while(messageReceived == "") {
28+
while(messageReceived == noMessageStr && !failedToConnect) {
2529
// NSLog("waiting for message")
2630
}
2731

2832
withReply(messageReceived)
2933

30-
messageReceived = ""
34+
messageReceived = noMessageStr
35+
failedToConnect = false
3136
}
3237

3338
func websocketDidConnect(socket: WebSocket) {
34-
NSLog("websocket is connected")
39+
NSLog("Websocket is connected")
3540
//socket.write(string: "getCommand")
3641
}
3742

3843
func websocketDidDisconnect(socket: WebSocket, error: NSError?) {
3944
if let e = error {
40-
NSLog("websocket is disconnected: \(e.localizedDescription)")
45+
NSLog("Websocket is disconnected: \(e.localizedDescription)")
4146
} else {
42-
NSLog("websocket disconnected")
47+
NSLog("Websocket disconnected")
4348
}
49+
50+
failedToConnect = true;
4451
}
4552

4653
func websocketDidReceiveMessage(socket: WebSocket, text: String) {

0 commit comments

Comments
 (0)