当前位置:K88软件开发文章中心编程语言JavaScriptCoffeeScript → 文章内容

CoffeeScript 双向客户端

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-15 15:34:06

由 珍珍阿姨 创建,Carrie 最后一次修改 2016-08-12 双向客户端问题你想通过网络提供持续的服务,与客户保持持续的联系。解决方案创建一个双向TCP客户机。在 Node.js 中net = require 'net'domain = 'localhost'port = 9001ping = (socket, delay) -> console.log "Pinging server" socket.write "Ping" nextPing = -> ping(socket, delay) setTimeout nextPing, delayconnection = net.createConnection port, domainconnection.on 'connect', () -> console.log "Opened connection to #{domain}:#{port}" ping connection, 2000connection.on 'data', (data) -> console.log "Received: #{data}"connection.on 'end', (data) -> console.log "Connection closed" process.exit()使用示例可访问Bi-Directional Server:$ coffee bi-directional-client.coffeeOpened connection to localhost:9001Pinging serverReceived: You have 0 peers on this serverPinging serverReceived: You have 0 peers on this serverPinging serverReceived: You have 1 peer on this server[...]Connection closed讨论这个特殊示例发起与服务器联系并在@connection.on 'connect'@处理程序中开启对话。大量的工作在一个真正的用户中,然而@connection.on 'data'@处理来自服务器的输出。@ping@函数递归是为了说明连续与服务器通信可能被真实的用户移除。另请参阅Bi-Directional Server,Basic Client和Basic Server。练习为选定的目标域和基于命令行参数或配置文件的端口添加支持。

CoffeeScript 双向客户端