Hello all,
I have a project, which is running Web server ,UDP client and few GPOs on threads.
The webserver has automatic data refresh on every 5 seconds. When its running on 1 tab it works fine. But when I open multiple tabs (3-4), it crashes. here is part of my code.
Thanks
Best regards
Dan
int main()
{ printf("Basic HTTP server example\n");// for testing only
iap.init();
//test_iap();
serial_load();
read_iap();
set_IP_address();
ethlink.mode(PullNone);
ethspd.mode(PullNone);
// Crete Threads
Thread UDPtx_rx; // Crete UDP tread
Thread TCPweb;
Thread ethled;
//UDP send receive
UDPtx_rx.start(UDP_rx);
TCPweb.start(webpage_load);
ethled.start(eth_led);
while (true) {
}
}
/*********************TCPServer srv/TCP socket******** */
TCPSocket srv;
TCPSocket *client_sock;
SocketAddress client_addr;
void webpage_load(){
char *web_recv_buf=new char[1024];//[1024];
printf("TCP WEB server\n");// for testing only
/* Open the server on ethernet stack */
srv.open(ð);
/* Bind the HTTP port (TCP 80) to the server */
srv.bind(eth.get_ip_address(), 80);
/* Can handle x simultaneous connections */
srv.listen(10);
while(true)
{
client_sock = srv.accept(); //return pointer of a client socket
client_sock->getpeername(&client_addr); //this will fill address of client to the SocketAddress object
nsapi_size_or_error_t size = client_sock->recv(web_recv_buf, 1024);
// printf("web recv 1==%d\n",size);// for testing only
if (size==536) {
nsapi_size_or_error_t size = client_sock->recv(web_recv_buf + 536, 1024);
//printf("web recv 2==%d\n",size);// for testing only
}
if(size>0){
std::string web_recv_str=web_recv_buf;
//**********check recved******** */
string web_chk_str=web_recv_str.substr(0,14);
char buf1[16];// for testing only
if (web_chk_str=="GET / HTTP/1.1")
{
http_response();
page_load();
led1=1;
led2=0;
}
else if (web_chk_str=="GET /tlystatus") {
http_response();
char *tly_send=new char[128];
sprintf(tly_send,"{\"tly_sts\":[%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"]}",tly_status[0],
tly_status[1],tly_status[2],tly_status[3],tly_status[4],tly_status[5],tly_status[6],tly_status[7],tly_status[8],tly_status[9],tly_status[10],tly_status[11],tly_status[12],tly_status[13],tly_status[14],
tly_status[15],UMD_WEB[0],UMD_WEB[1],UMD_WEB[2],UMD_WEB[3],UMD_WEB[4],UMD_WEB[5], UMD_WEB[6],UMD_WEB[7],UMD_WEB[8],UMD_WEB[9],UMD_WEB[10],UMD_WEB[11],UMD_WEB[12],UMD_WEB[13],UMD_WEB[14],UMD_WEB[15]);
client_sock->send(tly_send,strlen(tly_send));
delete [] tly_send;
}
}// end sze IF
//************************close socket clear buffer******************************
client_sock->close();
/*********Clear Buffers**********/
for(int i=0;i<1024;i++){
web_recv_buf[i]='\0';
}
} //end while
} // end main
JS code on web page
window.setInterval(function(){
nocache = "&nocache=" + Math.random() * 10;
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (this.readyState == 4) {
if (this.status == 200) {
if (this.responseText != null) {
var obj = JSON.parse(this.responseText)
//document.getElementById("testdata2").innerHTML = this.responseText;
var chkbox=document.getElementsByName("chkbox");
var chkbx=[];var i=0;var j=0;
for(;i<64;i++){
chkbx[i]=obj.tly_sts[j]&1;
chkbx[i+1]=obj.tly_sts[j]&2;
chkbx[i+2]=obj.tly_sts[j]&4;
chkbx[i+3]=obj.tly_sts[j]&8;
i=i+3;j=j+1;}
for (var j=0; j<64; j++){
if(0<(chkbx[j])){chkbox[j].style.outlineColor="red"}
else{chkbox[j].style.outlineColor="grey"}
if(0<(chkbx[j+1])){chkbox[j+1].style.outlineColor="lime"}
else{chkbox[j+1].style.outlineColor="grey"}
if(0<(chkbx[j+2])){chkbox[j+2].style.outlineColor="orange"}
else{chkbox[j+2].style.outlineColor="grey"}
if(0<(chkbx[j+3])){chkbox[j+3].style.outlineColor="blue"}
else{chkbox[j+3].style.outlineColor="grey"}
j=j+3;}
document.getElementById("U1").value=obj.tly_sts[16];
document.getElementById("U2").value=obj.tly_sts[17];
document.getElementById("U3").value=obj.tly_sts[18];
document.getElementById("U4").value=obj.tly_sts[19];
document.getElementById("U5").value=obj.tly_sts[20];
document.getElementById("U6").value=obj.tly_sts[21];
document.getElementById("U7").value=obj.tly_sts[22];
document.getElementById("U8").value=obj.tly_sts[23];
document.getElementById("U9").value=obj.tly_sts[24];
document.getElementById("U10").value=obj.tly_sts[25];
document.getElementById("U11").value=obj.tly_sts[26];
document.getElementById("U12").value=obj.tly_sts[27];
document.getElementById("U13").value=obj.tly_sts[28];
document.getElementById("U14").value=obj.tly_sts[29];
document.getElementById("U15").value=obj.tly_sts[30];
document.getElementById("U16").value=obj.tly_sts[31];
}}}}
request.open("GET", "tlystatus=?"+nocache, true);
request.send(null);
}, 5000);