这么愚蠢的问题,对不起。所以我下载了node.js/sockets.io/express.js。尝试制作一个简单的多人游戏,但目前从 hello world 开始。
我有以下目录结构:
-index.html
-server.js
-package.json
-node_modules/
server.js 有以下代码:
var app = require('express');
var http = require('http');
var io = require('socket.io');
/*
var server = http.createServer(function(req,res){
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('<h1>Hello world</h1>');
}).listen(80);
*/
var server = http.createServer(app);
server.listen(80);
var socket = io.listen(server);
var usersCount = 0;
socket.on('conection', function(client){
++usersCount;
console.log('client connecting... client# = ' + userCount);
client.emit('onMessageReceived',{msg:"You are client number: " + userCount});
});
我的客户端代码在 index.html 中,它的内容是:
<html>
<head>
<title> Client Code </title>
<script src="/socket.io/socket.io.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"> </script>
</head>
<body>
<script>
$(document).ready(function(){
var socket = new io.Socket({'127.0.0.1',{port:80}});
socket.connect();
socket.on('connect', function(obj){
console.log('client connection successful..');
});
socket.on('onMessageReceived', function(data){
console.log("client has received message: " + data.msg);
});
});
</script>
</body>
我的 package.json 包含:
{
"name": "hello_world",
"version": "0.0.0",
"description": "hello world test program",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": "",
"author": "",
"license": "BSD",
"dependencies":{
"express":"3.0.5",
"http": "",
"socket.io":""
}
}
现在我不确定如何继续运行该应用程序,如果我已经完成的话。关于下一步如何进行的任何帮助。我猜现在我需要让 server.js 不断运行,并让多个客户端打开 index.html 以连接到 server.js。