套接字无法加载资源服务器(404错误)

时间:2021-04-02 23:58:18

hey guys m using to files for server side server.js and at client side using javascript

嘿伙计们使用javascript在服务器端server.js和客户端使用文件

server.js

server.js

var express = require("express");
var io = require("socket.io").listen(app);
var app = express.createServer("localhost");
app.listen(3000);


app.get('/', function (req, res) {
    res.sendfile(__dirname + '/hello.html');
});

    console.log("before");
    io.sockets.on("connection", function (socket) {
        console.log("after");
        socket.on("message send", function (data) {
            io.sockets.emit("new mesage", data);
        });
    });

and at client side using one html page name

并在客户端使用一个html页面名称

hello.html

hello.html的

<html>
<head>
    <style>
        #chat{
        height : 500px;
        }

    </style>

</head>
<body>
    <div id="chat" > </div>
    <form id ="send message"> 
        <input id="message" type="text" ></input>
        <input type="submit"></input>
    </form>
    <script type= "javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script src="/socket.io/socket.io.js"></script>

    <script>
    jQuery(function($){
        var socket =io.connect();
        var $messageForm = $("#send message");
        var $messageBox=$("#message");
        var $chat = $("#chat");

            $messageForm.submit(function(e){
                e.preventDefault();
                socket.emit("message send" , $messageBox.val());
            });
            socket.on("new message", function(data){
                $chat.append(data + "</br>");   
            });
    });
    </script>

</body>
</html>

when m start server then its proporly working but when i sent a request then it shows error like given blow

当m启动服务器然后它的正常工作,但当我发送一个请求,然后它显示错误像给定的打击

Failed to load resource: the server responded with a status of 404 (Not Found) /socket.io/socket.io.js

无法加载资源:服务器响应状态为404(未找到)/socket.io/socket.io.js

Uncaught ReferenceError: jQuery is not defined

未捕获的ReferenceError:未定义jQuery

versions

版本

socket.io:0.9.16

socket.io:0.9.16

exparess:3.4.0

exparess:3.4.0

thanks for your time :)

谢谢你的时间 :)

1 个解决方案

#1


3  

You need to bind the socket.io to the http server not the application server. Changes this to below..

您需要将socket.io绑定到http服务器而不是应用程序服务器。将此更改为以下..

var express = require('express'),
    app = express(),
    http = require('http'),
    server = http.createServer(app),
    io = require('socket.io').listen(server);

Refer Express guide for more info.

有关详细信息,请参阅快速指南

EDIT

编辑

To correct the Jquery error..

要更正Jquery错误..

replace

更换

jQuery(function($){

with

$(document).ready(function($)

#1


3  

You need to bind the socket.io to the http server not the application server. Changes this to below..

您需要将socket.io绑定到http服务器而不是应用程序服务器。将此更改为以下..

var express = require('express'),
    app = express(),
    http = require('http'),
    server = http.createServer(app),
    io = require('socket.io').listen(server);

Refer Express guide for more info.

有关详细信息,请参阅快速指南

EDIT

编辑

To correct the Jquery error..

要更正Jquery错误..

replace

更换

jQuery(function($){

with

$(document).ready(function($)