在文档中,有一个通过路径创建套接字的函数, 它是socket.connect(path [,connectListener]) link 我试过这个: var net = require('net'), fs = require('fs'), path = require('path'), sock, os = require('os');const TEMP_DIR
它是socket.connect(path [,connectListener])
link
我试过这个:
var net = require('net'), fs = require('fs'), path = require('path'), sock, os = require('os'); const TEMP_DIR = os.tmpDir(); const TEMP_FILE = path.join(TEMP_DIR, 'my.sock'); console.log(TEMP_FILE) fs.open(TEMP_FILE, 'w+', function(err, fdesc){ if (err || !fdesc) { throw 'Error: ' + (err || 'No fdesc'); } sock = new net.Socket({ fd : fdesc, allowHalfOpen: true, readable: true, writable: true }); });
但是得到这个错误:
net.js:32 throw new TypeError('Unsupported fd type: ' + type); ^ TypeError: Unsupported fd type: FILE at createHandle (net.js:32:9) at new Socket (net.js:128:20) at /Users/timaschew/dev/myproject/test.js:20:12 at FSReqWrap.oncomplete (fs.js:82:15)
我也尝试过其他标志:w,a,a,但同样的erorr.
我在OSX 10.11.13上使用节点v4.2.0
我正在寻找一种不使用端口和本机节点模块的解决方案.如果它在linux,osx和windows上工作也会很好.
你应该使用server.listen(PATH)
:
const net = require('net'); const path = require('path'); const os = require('os'); const TEMP_DIR = os.tmpDir(); const TEMP_FILE = path.join(TEMP_DIR, 'my.sock'); net.createServer(...).listen(TEMP_FILE);
socket.connect()是连接到现有的域套接字(换句话说,实现客户端).