所以我想創建一個使用socket.io的node.js服務器,但我更多的是初學者,並且發現express包在句法上相當混亂。我知道我應該學習如何使用express和app.js,但計劃在達到node.js僅包含http,js和socket.io的能力範圍之後這樣做。我在網上查了將近一個小時,沒有任何東西可以幫助我解決問題。我在socket.io中遇到的主要問題是在客戶端,函數io();不管用。我甚至嘗試將body標籤上方的腳本鏈接路徑重定向到我的服務器項目目錄中的一個文件,但這只是返回一個錯誤,說require();不是一個功能。我在這段文本中包含了一些我正在使用的文件(但不是全部)。如果格式不正確或者其他不正確,請原諒我,因爲這是我第一次使用堆棧交換來提出問題。出於這個原因,如果你有足夠的聲譽來編輯它,使它更適合你在這裏遵循的格式,請做。使用node.js的socket.io完全沒有任何表達?
首先,我的服務器上的文件:
const http = require('http');
const fs = require('fs');
const io = require('socket.io')(http);
function socketReq(soc){
soc.emit("test", {"user":"test", "text":"testing da socket"});
}
io.on("connection", socketReq);
function server(req,res){
console.log('A user tried to connect to mazeserver.localtunnel.me'+req.url)
if(req.url == '/'){
console.log('Sending html...');
res.writeHead(200, {"Context-Type":"text/html"});
fs.createReadStream('./index.html').pipe(res);
}else if(req.url == '/pong.js'){
console.log('Sending JS...');
res.writeHead(200, {"Context-Type":"text/JavaScript"});
fs.createReadStream('./pong.js').pipe(res);
}else {
console.log('Error 404: file .'+req.url+' not found');
res.writeHead(404, {"Context-Type":"text/html"});
fs.createReadStream('./404.html').pipe(res);
}
}
http.createServer(server).listen(1337);
console.log('Server created');
而現在,我的主要HTML文件:
<html id='html'>
<head>
<title>Maze server</title>
<center>
<hr><br>
<h1>Welcome to my test server!</h1>
<br><hr>
<p>This is some text to test</p>
</center>
<script type='text/JavaScript'>
document.getElementById('html').style.hide = "true";
document.onload = function(){
document.getElementById('html').style.hide = "false";
}
var clicks = 0;
function clickButton(){
clicks++;
document.getElementById('clicks').innerHTML = clicks;
}
</script>
</head>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
</script>
<body>
<center>
<button onClick='clickButton();'>Im a button</button>
<p>You've clicked me <span id='clicks'>0</span> times. Ouch!</p>
<br><hr>
<p>Test canvas:</p>
<canvas id='canvas' width='400' height='200'></canvas>
<script src='pong.js' type='text/JavaScript'></script>
<br>
<br>
<button onClick='bounce();'>Bounce!</button>
<br>
<button onClick='speed("x");'>Speed up X</button>
<button onClick='speed("y");'>Speed up Y</button>
<br>
<button onClick='speed("x"); speed("y");'>Speed up both</button>
<br>
<button onClick='resetXY();'>Reset</button>
<br><hr>
</center>
<p>"Copyrite" <strong><blink type='EasterEgg' mazeiness='true'>MazeOfEncryption</blink></strong> 2048. Because why the heck not.</p>
</body>
</html>
如果需要更多的文件,請告訴我,我應該包括哪些文件,我雖然做假定問題出現在這兩個文件中的一箇中。在此先感謝,
-Maze
你可以看看socket.io規範,找出辦法。因爲附加到'express',它會添加幾條路由來處理一些握手POST和GET請求。 –
所以基本上我需要添加一些東西,使其無需明確地正確工作?你可以再詳細一點嗎? – Programah
在這裏的socket.io文檔中:http://socket.io/docs/#using-with-node-http-server。 Socket.io與普通的http服務器,並沒有明確表示。或者甚至沒有創建自己的Web服務器:http://socket.io/docs/#using-it-just-as-a-cross-browser-websocket(socket.io自己創建一個)。僅供參考,如果您需要處理socket.io以外的URL,則Express實際上非常簡單。如果沒有,那麼你可以在socket.io中使用普通的http服務器。 – jfriend00