你想在網(wǎng)絡(luò)上提供一個服務(wù)器。
創(chuàng)建一個基本的 TCP 服務(wù)器。
net = require 'net'
domain = 'localhost'
port = 9001
server = net.createServer (socket) ->
console.log "Received connection from #{socket.remoteAddress}"
socket.write "Hello, World!\n"
socket.end()
console.log "Listening to #{domain}:#{port}"
server.listen port, domain
可訪問 Basic Client:
$ coffee basic-server.coffee
Listening to localhost:9001
Received connection from 127.0.0.1
Received connection from 127.0.0.1
[...]
函數(shù)將為每個客戶端新連接的新插口傳遞給 @net.createServer@ 。基本的服務(wù)器與訪客只進(jìn)行簡單地交互,但是復(fù)雜的服務(wù)器會將插口連上一個專用的處理程序,然后返回等待下一個用戶的任務(wù)。
另請參閱 Basic Client,Bi-Directional Server 和 Bi-Directional Client。