使用来自iOS的Socket.io-stream将文件上传到Node.js服务器

时间:2014-02-05 06:33:01

标签: ios iphone objective-c node.js socket.io

我正在编写一个本机iOS客户端,使用Socket.IO-objc wrapper与Node.js服务器进行交互。我现在能够将个别事件发送到服务器就好了。但是,服务器正在使用Socket.IO-stream来处理上传文件流,我需要让我的iOS代码以某种方式与它进行交互。我的socket.io包装器似乎没有任何相关的API来实现这一点。接收server.js代码如下:

io.sockets.on('connection', function (socket) {
    console.log("connection occurring");
    ss(socket).on('uploadFile', function (stream, data, callback) {
      //...I can't get a 'stream' to this event for it to work with
    });
    socket.on('passwordLogin', function (data, callback) {
      //...I can cause these kind of events just fine
    });
    //...other event declarations
 }

我通常以这种方式调用事件,它适用于'socket.on'声明的事件:

[_socket sendEvent:@"passwordLogin" 
          withData:@{...login info...} 
    andAcknowledge:aCallbackBlock];

我想我需要扩展Socket.IO-objc,除非有一些我缺少的东西,例如:

//TODO: Attempt to expand SocketIO to include streams.
- (void) sendEvent:(NSString *)eventName withStream:(NSStream*) stream withData:(id) data   andAcknowledge:(SocketIOCallback) function
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObject:eventName forKey:@"name"];

    // do not require arguments
    if (data != nil) {
        [dict setObject:[NSArray arrayWithObject:data] forKey:@"args"];
    }

    SocketIOPacket *packet = [[SocketIOPacket alloc] initWithType:@"event"];
    packet.data = [SocketIOJSONSerialization JSONStringFromObject:dict error:nil];
    packet.pId = [self addAcknowledge:function];
    //Is this even a valid approach?
    packet.stream = stream;//<--NEW BIT
    if (function) {
       packet.ack = @"data";
    }
    [self send:packet];
}

我很遗憾究竟要做什么,以前从未真正使用过服务器通信。我已经关注了有关内置NSStream和CFStream对象的Apple文档,但它似乎并没有让我更接近与服务器事件的交互。任何帮助都表示赞赏,因为我有点想到我的结局。

0 个答案:

没有答案