javascript - socket.io websocket is closed before the connection is established -
console in browser prints me messege when connect server. i'm still able emit events server side , cach them on client side, unable emit them client side.
server side:
var express = require('express'); var http = require('http'); var app = express(); var server = http.createserver(app); var io = require('socket.io').listen(server); server.listen(8080); app.get("/", function(req, res) { res.sendfile(__dirname + '/index.html'); }); io.on('connection', function(socket) { socket.emit('news', { hello: 'world' }); socket.on('my other event', function (data) { console.log(data); }); client side:
<!doctype html> <html> <head> <meta charset="utf-8"> <title>untitled document</title> </head> <body> <h1 id ="id">tutaj jestem</h1> <script src="https://cdn.socket.io/socket.io-1.3.7.js"></script> <script> var socket = io.connect('http://localhost:8080'); socket.on('news', function (data) { console.log(data); socket.emit('my other event', { my: 'data' }); }); window.onload=socket; </script> </body> </html>
Comments
Post a Comment