当我向客户端使用forloop的服务器发送消息时,我在node.js中收到错误,我不知道为什么我会在客户端使用forloop时获得密切连接。那么有没有办法在错误事件上再次打开连接?
socket.on('error',function(err){
// I want to open the connection again here.
});
这就是我将消息发送到服务器的方式。
首先我使用ajax
msg.forEach(function (msgid) {
$.ajax({
type: 'get',
data: 'msg=' + msgid,
url: 'tomyclient',
success:function(data){
//response from server data
}
});
});
tomyclient.php
if(isset($_GET['msg'])) {
$sendmessage = $_GET['msg'];
$host = "localhost";
$port = 3030;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Could not create socket\n");
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1);
$result = socket_connect($socket, $host, $port) or die("Couldn't connect to server: [$errorcode] $errormsg");
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 60, "usec" => 0));
socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n");
$serverresponse= socket_read($socket, 1024,PHP_NORMAL_READ) or die("Could not read server response: [$errorcode] $errormsg");
echo $serverresponse;
}
这是我得到的错误
[Error: write ECONNABORTED] code: 'ECONNABORTED', errno: 'ECONNABORTED', syscall: 'write'
[Error: read ECONNRESET] code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read'
[Error: read ECONNRESET] code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read'
提前谢谢。