小编Jul*_*ert的帖子

PHP事件源继续执行

我开始使用JavaScript EventSource对象在HTML5中使用push.我对PHP的工作解决方案非常满意:

$time = 0;
while(true) {
    if(connection_status() != CONNECTION_NORMAL) {
        mysql_close()
        break;
    }
    $result = mysql_query("SELECT `id` FROM `table` WHERE UNIX_TIMESTAMP(`lastUpdate`) > '".$time."'");
    while($row = mysql_fetch_array($result)) {
        echo "data:".$row["id"].PHP_EOL;
        echo PHP_EOL;
        ob_flush();
        flush();
    }
    $time = time();
    sleep(1);
}
Run Code Online (Sandbox Code Playgroud)

但突然间我的WebApp再也无法访问MySQL错误"连接太多".

事实证明,在JavaScript中关闭事件源后,MySQL连接不会关闭:

window.onload = function() {
    sse = new EventSource("push.php");
    sse.onmessage = function(event) {
        console.log(event.data.split(":")[1]);
    }
}
window.onbeforeunload = function() {
    sse.close();
}
Run Code Online (Sandbox Code Playgroud)

所以我猜PHP脚本不会停止执行.die();在客户端连接断开之前有没有办法调用函数(比如)?为什么我的脚本在调用.close();EventSource 后没有终止?!

感谢帮助! -

javascript php html5 server-sent-events

3
推荐指数
1
解决办法
8160
查看次数

CoreBluetooth Central - >外围设备

我对蓝牙通信很陌生.我的第一个项目打算将数据从iOS设备传输到BLEshield(小芯片).

为了测试我的中央代码,我决定设置一个iPhone作为外围设备(芯片将拥有的角色,一旦我拿到它)和一个iPad作为中心.

我可以连接设备,也可以将数据从外设发送到中心.这很容易:

- (void)startService {
    _readChar = [[CBMutableCharacteristic alloc] initWithType:[CBUUID ...] properties:CBCharacteristicPropertyNotify value:nil permissions:CBAttributePermissionsReadable];
    _writeChar = [[CBMutableCharacteristics alloc] initWithType:[CBUUID ...] properties:CBCharacteristicPropertyNotify value:nil permissions:CBAttributePermissionsWriteable];

    _service = [[CBMutableService alloc] initWithType:[CBUUID ...] primary:YES];
    [_service setCharacteristics:@[_readChar, _writeChar]];

    _peripheral = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil];
    [_peripheral addService:_service];

    [_peripheral startAdvertising:@{CBAdvertisementDataServiceUUIDKey: @[[CBUUID ...]], CBAdvertisementDataLocalNameKey: @"ServiceName"}];
}

- (void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didSubscribeToCharacteristic:(CBCharacteristic *)characteristic {
    [_peripheral updateValue:[@"HELLO WORLD" dataUsingEncoding:NSUTF8StringEncoding] forCharacteristic:_readChar onSubscribedCentrals:nil];
}
Run Code Online (Sandbox Code Playgroud)

但我不能让另一个方向发挥作用.要从中央发送数据,我有以下代码:

[_activePeripheral writeValue:[@"PONG" dataUsingEncoding:NSUTF8StringEncoding] forCharacteristic:_writeChar type:CBCharacteristicWriteWithoutResponse];
Run Code Online (Sandbox Code Playgroud)

我假设应该在外设上调用这些方法中的任何一个:

- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveReadRequest:(CBATTRequest *)request
- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteRequests:(NSArray …
Run Code Online (Sandbox Code Playgroud)

ios core-bluetooth bluetooth-lowenergy cbperipheral cbcentralmanager

3
推荐指数
1
解决办法
4717
查看次数

UIRefreshControl EXC_BAD_ACCESS

我正在尝试在UIViewController中实现UIRefreshControl.我不能使用UITableViewController,因为tableView只是我的viewController的一个部分.

在大多数情况下,这种解决方法就像魅力.但有时(随机发生)应用程序崩溃EXC_BAD_ACCESS code=1

- (void)viewDidLoad {
    [super viewDidLoad];

    UIRefreshControl * refCon = [[UIRefreshControl alloc] init];
    [refCon addTarget:self selector:@selector(refresh:) forControlEvent:UIControlEventValueChanged];
    [tableView addSubView:refCon];
}

- (void)refresh:(UIRefreshControl *)sender {
    [NSThred detachNewThreadSelector:@selector(doRefresh:) toTarget:self withObject:sender];
}

- (void)doRefresh:(UIRefreshControl *)sender {
    [self checkUpdate];
    [self loadData];
    [sender endRefreshing];
}
Run Code Online (Sandbox Code Playgroud)

exc-bad-access uitableview uiviewcontroller uirefreshcontrol

2
推荐指数
1
解决办法
1038
查看次数

jQuery使用href属性创建一个

我刚刚遇到了一个非常严重的jQuery问题.我想用jQ创建一个元素.像我尝试的常规方式一样:jQuery('<a href="http://www.google.com"></a>');

但是,这并不能正常工作.所以我尝试了几种不同的元素类型和属性.事实证明,我无法创建包含HTML的元素href="".

  • jQuery('<a id="id5" name="link"></a>'); 将工作
  • jQuery('<a id="id5" name="link" href=""></a>');无法正常工作.

这个问题是否有任何已知问题或解决方法?谢谢你的建议......

编辑:

澄清一下:我不想使用已知属性创建元素.我有一个函数返回一个准备好的HTML代码.我想使用此代码提取图像标记,然后获取其属性.我不知道返回的HTML是否是相同的结构.我只知道它包含一个图像标签.这是我之前使用的代码:

jQuery(html).find('img:first').attr('class').match(/\image-(\d+)\b/)[1]
Run Code Online (Sandbox Code Playgroud)

javascript anchor jquery href

2
推荐指数
2
解决办法
3万
查看次数

Cocoa HTTP POST请求[Mac OS X]

我花了很多时间研究使用Cocoa的POST请求.

我发现一些看起来不错的代码.我改变它就像它符合我的需要.但代码不起作用,我找不到bug,因为我对可可很新.

这是我的代码.

- (IBAction)sendForm:(id)sender
{
    NSLog(@"web request started");
    NSString *post = [NSString stringWithFormat:@"firstName=%@&lastName=%@&eMail=%@&message=%@", firstName.stringValue, lastName.stringValue, eMail.stringValue, message.stringValue];
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:@"myDomain/form.php"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if(theConnection)
    {
        webData = [[NSMutableData data] retain];
        NSLog(@"connection initiated");
    }
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [webData appendData:data];
    NSLog(@"connection received data"); …
Run Code Online (Sandbox Code Playgroud)

html cocoa xmlhttprequest httpresponse http-post

1
推荐指数
1
解决办法
4738
查看次数

RegEx排除一个或另一个字符

我想用RegEx 排除一个另一个角色.我有一个搜索模式的RegEx \[([^\[]+\]\=\>(.*).

我的问题是最后的捕获模式.后面的字符串>应该后跟逗号或右括号.

这是我的文字:Array([0]=>123,[1]=>Array([a]=>1,[b]=>2))我想得到:

// match 1
0 = 0
1 = 123

// match 2
0 = 1
1 = Array([a]=>1,[b]=>2)
Run Code Online (Sandbox Code Playgroud)

这是我的RegEx:\[([^\[]+)\]\=\>([^,\)]+)\)?但我得到:

// match 1
0 = 0
1 = 123

// match 2
0 = 1
1 = Array([a]=>1

// match 3
0 = b
1 = 2
Run Code Online (Sandbox Code Playgroud)

regex nsregularexpression

1
推荐指数
1
解决办法
282
查看次数

PHP"永久变量"

我目前正在使用WordPress插件.当调用短代码时,它支持短代码并创建DOM元素.

现在我的问题:我想识别元素.

因此,当第一次调用短代码时,应该返回类似的东西

<div class="myClass-0"></div>
Run Code Online (Sandbox Code Playgroud)

当它第二次被召唤时

<div class="myClass-1"></div>
Run Code Online (Sandbox Code Playgroud)

等等.

有关这个问题的任何想法?感谢帮助

朱利安.

php variables global-variables permanent

0
推荐指数
1
解决办法
2571
查看次数

(Objective-C/Cocoa)Caste Superclass保持不变

我正在研究NSURLConnection的子类.我添加了一些属性和功能.我实现了NSURLConnectionDelegate及其方法.

现在我需要将NSURLConnection(及其属性)传递给我的子类委托.我为此实现了一个委托协议.

这是一个代码示例:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    if([_delegate respondsToSelector:_didReceiveDataSelector]) {
        [_delegate performSelector:_didReceiveDataSelector withObject:connection];
    }
Run Code Online (Sandbox Code Playgroud)

现在我还需要返回我的子类属性.我试图简单地转换实例:

mySubClass *obj = (mySubClass *)connection;
obj.userInfo = self.userInfo;
Run Code Online (Sandbox Code Playgroud)

但事实证明NSLog(@"%@", NSStringFromClass([obj class]));,演员会返回一个对象,该对象的类仍然是NSURLConnection.

现在我想知道如何合并超类实例和我的子类属性的所有属性值.

感谢帮助!

cocoa delegates casting subclass objective-c

0
推荐指数
1
解决办法
88
查看次数