zoukankan      html  css  js  c++  java
  • EventSource (node.js 与 OC)

    node.js服务器代码:

    var http = require('http');
    
    http.createServer(function (req, res) {
        res.writeHead(200, { 'Transfer-Encoding': 'chunked', 'Content-Type': 'text/event-stream' });
     
        setInterval(function() { 
            var packet = 'event: hello_event
    data: {"message":"' + new Date().getTime() + '"}
    
    '; 
            res.write(packet); 
        }, 1000);
    }).listen(9009);
    

     OC代码(需要借助封装的类:EventSource)

        EventSource *source = [EventSource eventSourceWithURL:[NSURL URLWithString:@"http://127.0.0.1:9009/"]];
        [source onReadyStateChanged:^(Event *event) {
            NSLog(@"READYSTATE: %@", event);
        }];
    
        [source addEventListener:@"hello_event" handler:^(Event *e) {
            NSLog(@"%@: %@", e.event, e.data);
        }];
    
  • 相关阅读:
    think in java
    TASLock TTASLock
    多线程
    jenkins unable to delete file
    ubuntu sun-jdk
    py2exe
    memcached安装
    redis安装
    ubuntu安装ssh
    mysql远程访问
  • 原文地址:https://www.cnblogs.com/levy/p/5987946.html
Copyright © 2011-2022 走看看