Socket.io未连接(2)

时间:2018-12-06 17:13:46

标签: node.js laravel websocket socket.io

我正在使用Laravel,目录结构如下:

/example.com
    /server.js
    /node_modules
    /css
    /js
    /main/views/writemessage.blade.php
    /main/views/socket.blade.php
    /main/app/Http/Controllers/SocketController.php

我在server.js中的代码是:

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
io.set('origins', '*:*');
var redis = require('redis');
var cors = require('cors');
app.use(cors());

server.listen(8890);
io.on('connection', function (socket) {

  console.log("new client connected");
  var redisClient = redis.createClient();
  redisClient.subscribe('message');

  redisClient.on("message", function(channel, message) {
    console.log("mew message in queue "+ message + "channel");
    socket.emit(channel, message);
  });

  socket.on('disconnect', function() {
    console.log('Disconnected!!!');
    redisClient.quit();
  });

});

我在writemessage.blade.php中的代码是:

@extends('_template.default')
@section('main-content')
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js"></script>

<div class="container">
    <div class="row">
        <div class="col-lg-8 col-lg-offset-2" >
          <div id="messages" ></div>
        </div>
    </div>
</div>
<script>
    var socket = io.connect('https://example.com:8890', {secure: true});
    socket.on('message', function (data) {
        console.log('YES!!!!!!!!!!');
        $( "#messages" ).append( "<p>"+data+"</p>" );
      });
</script>

@endsection

我在socket.blade.php中的代码是:

@extends('_template.default')
@section('main-content')
    <div class="container">
        <div class="row">
            <div class="col-md-10 col-md-offset-1">
                <div class="panel panel-default">
                    <div class="panel-heading">Send message</div>
                    <form action="sendmessage" method="POST">
                        <input type="text" name="message" >
                        <input type="submit" value="send">
                        {{csrf_field()}}
                    </form>
                </div>
            </div>
        </div>
    </div>

@endsection

当我在Windows中时,它返回CORS问题,但是现在当我在使用Linux时,它不返回该错误,但是在控制台中不返回任何内容。 Redis也正在运行...但是当我运行node server.js时,它甚至不会在控制台中显示已连接或已断开消息。我的问题来自哪里?是来自网址吗?我之所以说url,是因为当我在firefox中检查网络监视器时,我看到它是在说The connection used to fetch this resource was not secure。我做错什么了吗?还是我应该做更多的事情?如何测试问题的根源?

顺便说一句,我的节点版本:6.14.3和我的npm版本:3.10.10

谢谢

0 个答案:

没有答案