node.js中的Websocket聊天错误socket.io

时间:2018-02-13 06:52:17

标签: javascript node.js websocket socket.io

欢迎大家! 我已经和websocket聊天了,最棒的是它包含了php,为此我以不同的方式安装了这个websocket(服务器端)。

问题如下,当我运行服务器时,我从localhost / path打开页面..浏览器出错,

"未捕获的ReferenceError:io未定义"在: " var socket = io();"来自index.php,chat1.rar文件

服务器端似乎可以正常工作, node chat.js

我已经看了一周如何解决它,

我在下面提到的代码下面。

chat.js(服务器端)

var express = require( 'express' );
var http = require('http');
var socket = require('socket.io');
var bodyParser = require('body-parser')
var PORT = process.env.PORT || 8080;
var fs= require('fs');
var app = express();
var server = http.createServer( app );
var io = socket.listen( server );

io.on('connection',function(socket){
    var channel = 'channel-a';

    socket.join(channel);
    socket.on('message',function(msj){
        io.sockets.in(channel).emit('message',msj,socket.id);
    });
    socket.on('disconnect',function(){
        console.log("Desconectado : %s",socket.id);
    });

});
server.listen( 1334, function() {
    console.log('lisning to port 1334');
});

和index.php(客户端)

<!DOCTYPE html>
<html lang="en">
<head>

    <script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script>
</head>
<body>
    <div class="container">

    <script type="text/javascript">
     var socket = io.connect( 'http://localhost:1334' );
        var socket = io();
        $(function(){
            $("form").submit(function(){
                var mensaje = $("#msg").val();
                if(mensaje=='') return false;
                if(mensaje==' ') return false;
                socket.emit('message',mensaje);
                $("#msg").val('').focus();
                return false;
            });
        });
        socket.on('message',function(msg,id){
            //output.innerHTML += '<p><strong>' + id + ': </strong>' + msg + '</p>';
            $('#message').append($('<p><strong>' + id + ': </strong>' + msg + '</p>'));
            //$('#message').append($('<div class="msg new-chat-message">').text(id+': '+msg));
            //$('#message').append($('<div class="msg new-chat-message">').html('<span class="member-name">' + id + '</span>: ' + msg));
            $('.chat-window').scrollTop($('#message').height());
            //$("#message").append($('<li>').text(id+' : ' +msg));
        });
        socket.on('change channel',function(channel){
            $("#message").html('').append($('<li>').text('system : Bienvenido al Canal '+channel+' !'));
        });
    </script>

    <div class="col-md-12">
        <div class="chat-window">
              <div id="message"></div>
    </div>
    <div id="controls">
        <form action="">
            <select name="channel" id="channel">
                <option value="channel-a">Channel A</option>
            </select>

            <div class="col-md-12">
                <div class="input-group enter-chat-message">
                    <input type="text" id="msg" class="form-control" placeholder="Chat Message...">
                    <input class="input-group-addon submit-chat-message" type="submit" id="btn" value="Enviar">
                </div>
            </div>
</div></div>
        </form>
    </div></div></div></div>
    <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script>
            <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" ></script>
            <script src="js/bootstrap.js"></script>
            <script src="node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js"></script>
</body>
</html>

截图: http://prntscr.com/ie030c

任何帮助都会得到赞赏! 感谢

1 个答案:

答案 0 :(得分:0)

改变这个:

<script src="node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js">

要:

<script src="/socket.io/socket.io.js">

看看会发生什么。