nativescript-simple-networking icon indicating copy to clipboard operation
nativescript-simple-networking copied to clipboard

tcpServer only works one time

Open ghost opened this issue 8 years ago • 1 comments

I'm sorry Honza, i have one more question (the last, i promisse).

The code for tcpServer works very well, but only one time. The server stop to response after call "onFinished" function. The app still running, but not receive or process the second packet.

Main Code:

import {UdpServer, TcpClient, TcpServer} from "nativescript-simple-networking";
import {Address4} from "ip-address";
import { EventData } from 'data/observable';
import { Page } from 'ui/page';
import { HelloWorldModel } from './main-view-model';
export function navigatingTo(args: EventData) {
let page = <Page>args.object;
page.bindingContext = new HelloWorldModel();
tcpServer.start(44444);
}

// Start a TCP server listening on port 44444 with maximum 2 clients
var tcpServer = new TcpServer(3);
tcpServer.onClient = (client: Address4) => {
console.log("New TCP cliente: ", client.adddress);
}; 

tcpServer.onData = (client: Address4, data: string) => {
console.log("New data from client ", client.address, ": ", data);
tcpServer.send(client, "HTTP/1.0 200 OK\r\nContent-Type: text/plain; charset=utf-8\r\nDate: Sun, 29 Out 2017 05:00:48 GMT\r\nContent-Length: 5\r\nConnection: close\r\n\r\nApp:" + Math.floor(Math.random() * 10) + 1 );
};
tcpServer.onError = (id: number, client: Address4, message: string) => {
if (client)
console.log("TCP server client error", client.address, ": ", message);
else
console.log("TCP server error: ", message);
};
tcpServer.onFinished = (id: number) => {
console.log("TCP server finished transaction #", id);
};

Browser output: First access: image

Generate on console: image

Second access (refreshing page): The browser start a continuous loop. image

Console (no change): image

What I've tried: reload tcpServer inside onFinished function. Change header http to keep-alive. Increase the number of clients from 2 to 99 (up to 99 request from one client?)

ghost avatar Nov 01 '17 02:11 ghost

The behaviour you are describing is strange. I was not able to reproduce it on my device.

yaqwsx avatar Nov 15 '17 11:11 yaqwsx