我正在尝试构建一个基本的聊天html程序。 我已经设置了一个节点服务器来测试它,但是,当访问html页面时,我收到一个套接字错误。这是我第一次使用node.js并设置节点服务器,所以我肯定做错了。
感谢所有看过这个的人!
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require("socket.io").listen(server);
var socket = io.listen(1223, "1.2.3.4");
server.listen(process.env.PORT || 3000);
console.log('Server is running...');
var people = {};
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
})
// When connecting
socket.on("connection", function(client) {
client.on("join", function(name){
people[client.id] = name;
client.emit("update", "You have connected to the server.");
socket.sockets.emit("update", name + " has joined the server.");
socket.sockets.emit("update-people", people);
});
// When sending
client.on("send", function(msg){
socket.sockets.emit("chat", people[client.id], msg);
});
// When disconnecting
client.on("disconnect", function(){
socket.sockets.emit("update", people[client.id] + " has left the server.");
delete people[client.id];
socket.sockets.emit("update-people", people);
});
});

<!DOCTYPE html>
<html lang="en">
<head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
</head>
<script>
$(document).ready(function(){
var socket = io.connect("1.2.3.4:1223");
$("#chat").hide();
$("#name").focus();
$("form").submit(function(event){
event.preventDefault();
});
$("#join").click(function(){
var name = $("#name").val();
if (name != "") {
socket.emit("join", name);
$("#login").detach();
$("#chat").show();
$("#msg").focus();
ready = true;
}
});
$("#name").keypress(function(e){
if(e.which == 13) {
var name = $("#name").val();
if (name != "") {
socket.emit("join", name);
ready = true;
$("#login").detach();
$("#chat").show();
$("#msg").focus();
}
}
});
socket.on("update", function(msg) {
if(ready)
$("#msgs").append("" + msg + "");
})
socket.on("update-people", function(people){
if(ready) {
$("#people").empty();
$.each(people, function(clientid, name) {
$('#people').append("" + name + "");
});
}
});
socket.on("chat", function(who, msg){
if(ready) {
$("#msgs").append("" + who + " says: " + msg + "");
}
});
socket.on("disconnect", function(){
$("#msgs").append("The server is not available");
$("#msg").attr("disabled", "disabled");
$("#send").attr("disabled", "disabled");
});
// Sending the message (either by button click or enter)
$("#send").click(function(){
var msg = $("#msg").val();
socket.emit("send", msg);
$("#msg").val("");
});
$("msg").keypress(function(e){
if (e.which == 13) {
var msg = $("msg").val();
socket.emit("send", msg);
$("#msg").val("");
}
});
});
</script>
<body>
<div class="container">
<div class="row">
<div class="span2">
<ul id="people" class="unstyled"></ul>
</div>
<div class="span4">
<ul id="msgs" class="unstyled">
</div>
</div>
<div class="row">
<div class="span5 offset2" id="login">
<form class="form-inline">
<input type="text" class="input-small" placeholder="Your name" id="name">
<input type="button" name="join" id="join" value="Join" class="btn btn-primary">
</form>
</div>
<div class="span5 offset2" id="chat">
<form id="2" class="form-inline">
<input type="text" class="input" placeholder="Your message" id="msg">
<input type="button" name="send" id="send" value="Send" class="btn btn-success">
</form>
</div>
</div>
</div>
</body>
</html>
&#13;
答案 0 :(得分:0)
您混合了不同的class CarsController < JSONAPI::ResourceController
include JSONAPI::Utils
def index
@cars = Car.ransack(params[:q]).result
jsonapi_render json: @cars
end
end
来电。
listen
第三行没有你期望的效果。 var server = require('http').createServer(app);
var io = require("socket.io").listen(server);
var socket = io.listen(1223, "1.2.3.4");
server.listen(process.env.PORT || 3000);
不是在倾听自己。它使用正在侦听端口3000的http服务器。
而是在服务器上使用它
io
在客户端连接到端口3000(或通过PORT环境变量配置的端口。
var server = require('http').createServer(app);
var socket = require("socket.io").listen(server);
server.listen(process.env.PORT || 3000);
如果要在socket.io的专用端口上侦听,则必须创建第二个http服务器侦听第二个端口,并将其绑定到socket.io。