问题描述
我是 Socket.IO 的 100% 新手并且刚刚安装了它.我试图遵循一些示例,并且可以使服务器端运行,但似乎无法连接客户端.
I'm 100% new to Socket.IO and have just installed it.I was trying to follow some examples, and can get the server side running but I can't seem to get the client side connected.
以下是我的 server.js:
The following is my server.js:
var http = require('http'), io = require('socket.io'),
server = http.createServer(function(req, res){
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('<h1>Hello world</h1>');
});
server.listen(8090);
var socket = io.listen(server);
socket.on('connection', function(client){
console.log('Client connected.');
client.on('message', function(){
console.log('Message.');
client.send('Lorem ipsum dolor sit amet');
});
client.on('disconnect', function(){
console.log('Disconnected.');
});
});
这是我的 index.html
This is my index.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Socket Example</title>
<base href="/" />
<meta charset="UTF-8" />
<script src="http://localhost:8090/socket.io/socket.io.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
</head><body>
<script type="text/javascript">
$(document).ready(function(){
var socket = new io.Socket('localhost', { 'port': 8090 });
socket.connect();
console.log("Connecting.");
socket.on('connect', function () {
console.log("Connected.");
});
socket.on('message', function (msg) {
console.log("Message: " + msg + ".");
});
socket.on('disconnect', function () {
console.log("Disconnected.");
});
});
</script>
</body></html>
当我执行 node server.js 时,它表明 socket.io 已启动.
When I do node server.js it indicates that socket.io is started.
当我加载 index.html 时,会出现一行指示调试 - 服务静态/socket.io.js"但没有别的,没有控制台消息或其他行.
When I load the index.html a line comes up indicating "debug - served static /socket.io.js"But nothing else, no console messages or other lines.
非常感谢您能提供的任何指导.正如我所说,我 100% 环保,所以如果你能尽可能地分解它,我也会很感激.
Any guidance you could provide would be very much appreciated. As I said I'm 100% green with this so if you could break it down as much as possible I'd also appreciate it.
谢谢
推荐答案
在 同源策略, localhost:8090
和 localhost
不是同一个来源(不同端口),所以 localhost/index.html
无法连接到 socket.io 上本地主机:8090.
In same origin policy, localhost:8090
and localhost
are not the same origin (different port), so localhost/index.html
cannot connect to socket.io on localhost:8090.
一种选择是在 localhost:8090
上制作 index.html(见下面的代码).然后,您只需将index.html"放入服务器脚本的同一目录中,启动服务器并在浏览器中键入 localhost:8090
.
One option is to make index.html on localhost:8090
(see code below). Then you just put "index.html" into the same directory of your server script, start the server and type localhost:8090
in your browser.
var fs = require('fs');
server = http.createServer(function(req, res){
fs.readFile(__dirname + '/index.html', 'utf-8', function(err, data) { // read index.html in local file system
if (err) {
res.writeHead(404, {'Content-Type': 'text/html'});
res.end('<h1>Page Not Found</h1>');
} else {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(data);
}
});
});
这篇关于Socket.IO 基本示例不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!