Nor*_*rod 2 javascript request node.js socket.io npm-request
在我的服务器端,我监听套接字服务器,在我自己的笔记本电脑中,我具有socket.io-client服务,每当我打开它们时,它们都在连接。
当其他人向我的服务器请求时,服务器通过套接字将该请求发送到我的笔记本电脑,然后我的笔记本电脑使用npm-request从本地主机获取数据,并将返回的数据返回给服务器,然后服务器将信息显示给客户端。
这是我服务器端的错误:
/*
throw er; // Unhandled 'error' event
^
**Error [ERR_STREAM_WRITE_AFTER_END]: write after end**
at write_ (_http_outgoing.js:572:17)
at ServerResponse.write (_http_outgoing.js:567:10)
at Socket.<anonymous> (/public_html/api/index.js:37:9)
at Socket.emit (events.js:187:15)
at /public_html/api/node_modules/socket.io/lib/socket.js:528:12
at process._tickCallback (internal/process/next_tick.js:61:11)
Emitted 'error' event at:
at writeAfterEndNT (_http_outgoing.js:634:7)
at process._tickCallback (internal/process/next_tick.js:63:19)
*/
Run Code Online (Sandbox Code Playgroud)
服务器端代码:
/*
throw er; // Unhandled 'error' event
^
**Error [ERR_STREAM_WRITE_AFTER_END]: write after end**
at write_ (_http_outgoing.js:572:17)
at ServerResponse.write (_http_outgoing.js:567:10)
at Socket.<anonymous> (/public_html/api/index.js:37:9)
at Socket.emit (events.js:187:15)
at /public_html/api/node_modules/socket.io/lib/socket.js:528:12
at process._tickCallback (internal/process/next_tick.js:61:11)
Emitted 'error' event at:
at writeAfterEndNT (_http_outgoing.js:634:7)
at process._tickCallback (internal/process/next_tick.js:63:19)
*/
Run Code Online (Sandbox Code Playgroud)
并且请相信我在笔记本电脑上工作的客户端代码
var http = require('http');
var fs = require('fs');
var socket_g=null, numOfUsers=0;
var url = require('url'),qs = require('querystring');
var server = http.createServer(function(req, res) {
if(numOfUsers!=0){
var urlParts = url.parse(req.url, true),
urlParams = urlParts.query,
urlPathname = urlParts.pathname,
body = '',
reqInfo = {};
req.on('data', function (data) {
body += data;
});
req.on('end', function () {
reqInfo.urlPathname = urlPathname;
reqInfo.urlParams = urlParams;
reqInfo.body = qs.parse(body);
reqInfo.urlParts = urlParts;
console.log(reqInfo.urlPathname)
socket_g.emit('event', { "path": reqInfo.urlPathname});
});
socket_g.on('data',function(data){
console.log(data.c)
if(data.c=='application/x-httpd-php' || data.c=='/'){
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write(data.data);
res.end();
}else{
res.writeHead(200, { 'Content-Type': data.c });
res.write(data.data);
res.end();
}
});
}else{
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end("<h2>There is no client connected!</h2>");
}
});
var io = require('socket.io').listen(server);
io.sockets.setMaxListeners(0);
io.sockets.on('connection', function (socket) {
/*console msg*/
console.log('user connected!');
/*console msg*/
socket.setMaxListeners(0);
numOfUsers++;
socket_g=socket;
socket.on('disconnect', function(){
numOfUsers++;
/*console msg*/
console.log('user disconnected');
/*console msg*/
});
});
server.listen(3333);
Run Code Online (Sandbox Code Playgroud)
即使返回响应,您仍在监听“数据”事件。
用.once听的事件一次针对一个特定的请求。
socket_g.once("data", function(data) {
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6552 次 |
| 最近记录: |