node js wymiana danych typu klient serwer

0

Witam mam problem z Node JS.
Tutaj od razu zaznaczę, że nie jestem osobą, która biegle porusza się po JS, a kimś kto stara się przyswoić jak działa Node JS.

Więc chciałem wykonać prostą aplikacje serwera, która wyśle wiadomość do klienta, który się podłączy przez plik .html.

Prosty serwer w NodeJS (z Google).

var net = require('net');
var sockets = [];
var port = 8000;
var guestId = 0;

var server = net.createServer(function(socket) {
	// Increment
	guestId++;
	
	socket.nickname = "Guest" + guestId;
	var clientName = socket.nickname;

	sockets.push(socket);

	// Log it to the server output
	console.log(clientName + ' joined this chat.');

	// Welcome user to the socket
	socket.write("Welcome\n");

	// Broadcast to others excluding this socket
	//broadcast(clientName, clientName + ' joined this chat.\n');


	// When client sends data
	socket.on('data', function(data) {

		var message = clientName + '> ' + data.toString();
		broadcast(clientName, message);
		process.stdout.write(message);
	});


	// When client leaves
	socket.on('end', function() {

		var message = clientName + ' left this chat\n';

		// Log it to the server output
		process.stdout.write(message);

		// Remove client from socket array
		removeSocket(socket);

		// Notify all clients
		//broadcast(clientName, message);
	});


	// When socket gets errors
	socket.on('error', function(error) {

		console.log('Socket got problems: ', error.message);

	});
});


// Broadcast to others, excluding the sender
function broadcast(from, message) {

	// If there are no sockets, then don't broadcast any messages
	if (sockets.length === 0) {
		process.stdout.write('Everyone left the chat');
		return;
	}

	// If there are clients remaining then broadcast message
	sockets.forEach(function(socket, index, array){
		// Dont send any messages to the sender
		if(socket.nickname === from) return;
		
		socket.write(message);
	
	});
	
};

// Remove disconnected client from sockets array
function removeSocket(socket) {

	sockets.splice(sockets.indexOf(socket), 1);

};


// Listening for any problems with the server
server.on('error', function(error) {

	console.log("So we got problems!", error.message);

});

// Listen for a port to telnet to
// then in the terminal just run 'telnet localhost [port]'
server.listen(port, function() {
	console.log("Server listening at http://localhost:" + port);

});

Uruchamiam go odpalam 2x telnet łącze się pod serwer - wymiana danych ładnie działa i jest ok.

Teraz myślę sobie - pora podłączyć to pod plik .html

Tworze plik klient.html umieszczam kod:

<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
        <script src="socket.io-1.4.5.js"></script>
        
        <script>
            $(function(){
                var socket = io.connect('http://localhost:8000', {
                    'reconnection delay': 2000,
                    'force new connection': true
                });
                socket.on('connect', function () {
                    alert('connected');
                });
                socket.on('message', function (data) {
                    alert(data);
                });
            });
        </script>
    </head>
    <body>
        <div>TODO write content</div>
        
        <div class="text">
            
        </div>
    </body>
</html>

I liczyłem na to, że wyskoczy alert o nawiązaniu z połączeniem a zamiast tego w konsoli serwera widzę:

Guest25 joined this chat.
GET /socket.io/?EIO=3&transport=polling&t=LJ4gb39 HTTP/1.1
Host: localhost:8000
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: pl,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
Origin: null
Connection: keep-alive

I się zapętla Guest1... Guest2.... Guest3 i tak w koło.
Dlaczego tak się dzieje?

Prawdę mówiąc liczyłem na to, że podłącze się do serwera i nastąpi nasłuchiwanie i odbiór danych.

0

Problem jest taki, że nawet jak usunę te linijki od rozłączenia i połączenia to skrypt w klient.html jest martwy.
Alert nie wyskakuje nie można wysłać nic do serwera.

1 użytkowników online, w tym zalogowanych: 0, gości: 1