我正在尝试使用 SocketRocket 和 ws 在客户端(iOS 应用程序)和服务器(Node.js)之间进行连接,如下所示。
iOS(SocketRocket):
NSURL *url = [NSURL urlWithString:@"ws://localhost:8080"];
SRWebSocket *_socket = [SRWebSocket alloc] initWithURLRequest:[NSURLRequest requestWithURL:url];
_socket.delegate = self;
[_socket open];
/* SRWebSocketDelegate */
-(void)webSocketDidOpen:(SRWebSocket*)webSocket{
[webSocket send:@"something"];
}
-(void)webSocket:(SRWebSocket*)webSocket didReceiveMessage:(id)message{
NSLog(@"didReceiveMessage: %@",[message description]);
}
-(void)webSocket:(SRWebSocket*)webSocket didFailWithError:(NSError*)error{
NSLog(@"the Error: %@",error);
}
节点.js(ws):
var WebSocketServer = require('ws').Server
var wss = new WebSocketServer({
host:'localhost',
port:8080
});
wss.on('connection',function(ws){
ws.on('message',function(message){
console.log('received: %s', message);
ws.send(message);
});
});
然后,我收到以下消息:
the error: Error Domain=NSPOSIXErrorDomain Code=61 "The operation couldn’t be completed. Connection refused"
我一直在寻找解决这个问题的方法,但我找不到确切的解决方案。我该如何解决这个问题?