socket.io websocket在建立连接之前关闭

时间:2016-01-05 23:55:17

标签: javascript node.js websocket socket.io

当我与服务器连接时,浏览器中的控制台会打印此信息。我仍然可以从服务器端发出事件并在客户端缓存它们,但我无法从客户端发出它们。

服务器端:

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);
        });

客户端:

<!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>

0 个答案:

没有答案