I'm running a simple server that show time, and when someone connects to it using telnet\putty he can see the time.
I need to write a code in JavaScript that reads the data from the server. how do I do this ?
this is the server code
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Calendar;
class ClientThread extends Thread {
private final Socket _socket;
public ClientThread( Socket socket ) {
System.out.println( "New client" );
_socket = socket;
setDaemon( true );
start();
}
@Override
public void run() {
try(
final OutputStream outputFromServer = _socket.getOutputStream();
final PrintWriter serverPrintOut = new PrintWriter(
new OutputStreamWriter( outputFromServer, "utf-8" ), true ))
{
serverPrintOut.println( "Welcome to time server" );
for(;;) {
final long elapsed = System.currentTimeMillis() - MyServer.StartTime;
serverPrintOut.println( elapsed );
Thread.sleep( 1000L );
}
}
catch( final InterruptedException ex) {/**/}
catch( final IOException e ) {
e.printStackTrace();
}
}
}
public class MyServer {
static Calendar startTime = Calendar.getInstance();
static long StartTime = System.currentTimeMillis();
public static void main( String[] args ) throws IOException {
try( ServerSocket serverSocket = new ServerSocket( 9991 )) {
for(;;) {
new ClientThread( serverSocket.accept());
}
}
}
}
this is what I have in JavaScript
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<%--<script type="text/javascript" src="jsocket.js"></script>--%>
<%--<script type='text/javascript'>--%>
<%--// Host we are connecting to--%>
<%--var host = '10.0.0.82';--%>
<%--// Port we are connecting on--%>
<%--var port = 9991;--%>
<%--var socket = new JSocket();--%>
<%--// When the socket is added the to document--%>
<%--socket.onReady = function(){--%>
<%--socket.connect(host, port);--%>
<%--}--%>
<%--// Connection attempt finished--%>
<%--socket.onConnect = function(success, msg){--%>
<%--if(success){--%>
<%--// Send something to the socket--%>
<%--socket.write('Hello world');--%>
<%--}else{--%>
<%--alert('Connection to the server could not be estabilished: ' + msg);--%>
<%--}--%>
<%--}--%>
<%--socket.onData = function(data){--%>
<%--alert('Received from socket: '+data);--%>
<%--}--%>
<%--// Setup our socket in the div with the id="socket"--%>
<%--socket.setup('mySocket');--%>
<%--</script>--%>
<script src="socket.io.js"></script>
<script>
// var socket = io('http://10.0.0.82:9991');
// socket.on('connect', function(){});
// socket.on('event', function(data){});
// socket.on('disconnect', function(){});
var socket = io('10.0.0.82:9991');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
</head>
<body>
</body>
</html>
but when I run the JavaScript code I get error "Invalid http resonse" which I understand why(the server is not http) - but how do I overcome this ? what will be fast and easy - change the JavaScript or make the server http ?
- I do want to add that the final code will be handling 30 types of data (in the server) - all kind of sensor reading.