我正在尝试使用下面的socketrocket和ws在客户端(ios应用)和服务器(node.js)之间连接。
ios(socketocket):
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);
}
node.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"
我已经搜索了解决这个问题,但是我找不到确切的解决方案。我该如何解决?
连接到iPhone上的Wi-Fi网络。也许解决了这个问题。
将localhost
更改为您的实际IP地址,例如10.28.8.146
可以解决您的问题。