Reputation: 119
Im having issues with a node.js server and HTML client.
The server code is:
var http = require('http');
fs = require('fs');
var app = http.createServer(function(request, response) {
fs.readFile("client.html", 'utf-8', function (error, data) {
response.writeHead(200, {'Content-Type': 'text/html'});
response.write(data);
response.end();
});
}).listen(1337);
var io = require('socket.io').listen(app);
io.sockets.on('connection', function(socket) {
socket.on('message_to_server', function(data) {
io.sockets.emit("message_to_client",{ message: data["message"]});
});
});
console.log('Server running')
And client code is:
<!DOCTYPE html>
<html>
<head>
<title>Rumors and speculations</title>
<script src="/socket.io/socket.io.js"></script>
<script type="text/javascript">
var socketio = io.connect("localhost", {port: 1337});
socketio.on("message_to_client", function (data) {
document.getElementById("chatlog").innerHTML = ("<hr/>" +
data['message'] + document.getElementById("chatlog").innerHTML);
});
function sendMessage() {
var msg = document.getElementById("message_input").value;
socketio.emit("message_to_server", { message: msg });
}
</script>
</head>
<body>
<input type="text" id="message_input" />
<button onclick="sendMessage()">send</button>
<div id="chatlog"></div>
</body>
</html>
I don't know if it makes a difference or not, but i am running my HTML from Visual Studio (building an ASP.NET MVC app).
I get the chat button and windows for typing on the screen, but when i add some text and click 'send' nothing happens, and nothing is displayed. Am I missing something? I would almost bet that I am missing something in regards to the server adress, but I have stared myself blind to be able to find it.
Upvotes: 0
Views: 224
Reputation: 881
In your client code: data['message'] + document.getElementById("chatlog").innterHTML is spelled incorrectly. It should say .innerHTML
Upvotes: 1