Tag: 套接字

Node.js:如何刷新套接字?

我想在发送下一个数据块之前刷新套接字: var net = require('net'); net.createServer(function(socket) { socket.on('data', function(data) { console.log(data.toString()); }); }).listen(54358, '127.0.0.1'); var socket = net.createConnection(54358, '127.0.0.1'); socket.setNoDelay(true); socket.write('mentos'); socket.write('cola'); 但是,尽pipe有setNoDelay选项,但是,这不起作用,例如打印“mentoscola”而不是“mentos \ ncola”。 我该如何解决?

socket.IO-objc与AZSocketIO

从其他语言的Socket.IO实现列表中 ,我们可以看到Objective-C有两个替代scheme。 我的问题是关于这两个图书馆的优点和缺点。 socket.IO-objc似乎更完整,并有更好的文档,但我也想知道,如果这些优势足够重要,我的项目select一个或另一个AZSocketIO提供考虑哪些优势。 我真的很感激,如果有人使用其中的一个可以给我一些build议。 谢谢!

socket.io手动添加用户到空间(node.js)

我有一个私人工作的聊天应用程序。我的实现是: 1 – 用户进入另一个用户configuration文件页面并写入消息。 在此之后,我为这个对话创造了一个空间。 socket.join(data.user); 2 – 接收者用户将采取这个消息(只有这个用户),但我不知道我怎么可以join这个接收机用户到这个房间。 所以这是我的问题基本上。 我需要添加“手动”这个接收机用户到我的房间。 这样当我把消息广播到这个房间的时候,消息就会发出这两个用户(一个是匿名的,另一个是注册用户)。 它会提供我的一些代码。 server .on('connection', function(socket) { server.to(socket.id).emit('yourSocketId',{ socketid : socket.id,connectedDate : Date.now }); socket.on('AnonymousMessage', function(data) { if(data.isanon == true) { socket.join(data.user); server.to(data.user).emit('AnonymousBroadcast', { message: data.message, user: data.user, isanon : data.isanon, date : data.date, tabno : data.tabno }); } else { socket.join(****); //– I need […]

NodeJS:什么是处理TCP套接字stream的正确方法? 我应该使用哪个分隔符?

根据我在这里所理解的,“V8有一个世代垃圾收集器,随机移动对象,节点不能得到一个指向原始string数据的指针来写入套接字。 所以我不应该将来自TCPstream的数据存储在string中,特别是如果该string变得比Math.pow(2,16)字节更大。 (希望我到现在为止..) 那么什么是处理来自TCP套接字的所有数据的最佳方式? 到目前为止,我一直试图使用_:_:_作为分隔符,因为我认为它是某种独特的,不会混淆其他的东西。 数据的一个样本将会是something_:_:_maybe a large text_:_:_ maybe tons of lines_:_:_more and more data 这是我试图做的: net = require('net'); var server = net.createServer(function (socket) { socket.on('connect',function() { console.log('someone connected'); buf = new Buffer(Math.pow(2,16)); //new buffer with size 2^16 socket.on('data',function(data) { if (data.toString().search('_:_:_') === -1) { // If there's no separator in the data that […]

如何在sails.js 0.10中接收套接字事件?

我有一个名为Collection.js的模型,它看起来像这样: module.exports = { attributes: { title: { type: 'string', required: true }, description: { type: 'text', required: false }, sessions: { collection: 'session', via: 'collection' } } } 据我所知,这个消息是在创build新模型实例时通过套接字发送的,可以通过模型名称获得。 所以我已经在我的app.coffee文件中进行了app.coffee : socket = io.connect() socket.on 'collection', -> console.log 'hey' 我已经validation了“环境”的作用,因为我得到了socket.on 'connect'. -> console.log 'hey'通知socket.on 'connect'. -> console.log 'hey' socket.on 'connect'. -> console.log 'hey' ,但我没有得到任何模型事件,如创build或破坏logging! […]

socket.io 1.2.1如何在重新连接后解决双重事件

在socket.io的博客中说,在1.2.1版本中 “自述文件修复,以防止在重新连接的示例中的双重事件[@nkzawa]” 我为客户端和服务器下载了1.2.1版本,但是在重新连接之后仍然是两次调用事件。 或者我应该用自述文件做些什么? 我试过这种方式重新连接,它的工作,但我可以用它来生产。 这是不是很方便? socket.disconnect() // remove socket object socket = undefined // connect again socket = io.connect({'forceNew':true}) 正如我所说的是正确的方式,还是有缺点? 更新,添加代码 服务器端代码 socket.on('client_emited', function(data){ socketIO.to('this socket id').emit('server_emitted'); }) 客户端代码 var socket; function connectSocket () { if (!socket) socket = io({'forceNew':true}); else socket.connect(); } socket.on('connect', function(){ console.log('CONNECTED TO SOCKET.IO SERVER. SENDING "client_emited" EVENT'); socket.emit('client_emited'); }); […]

当服务器脱机时,socket.io发射

我正在使用socket.io v1.3.6(nodejs),并尝试使用下面的代码从浏览器发出数据。 客户代码 var socket = io.connect('http://something.com:3300/'); function sendMessage(message) { socket.emit('message', message); } 服务器代码 var io = require('socket.io').listen(3300); io.sockets.on('connection', function (socket) { messageHandler(socket); }); function messageHandler(socket) { socket.on('message', function (data) { console.log('Captured a message : ' + data); }); } 我的套接字服务器http://something.com:3300/最初是closures的,并试图调用几个sendMessage() – (约10个电话) 正如预期在浏览器中,我将得到控制台日志中的握手错误。 我等了5分钟,启动了套接字服务器。 但令人惊讶的是,一旦握手build立,在离线状态下发送的所有消息都将被捕获到服务器中。 我的问题:1)这是离线逻辑作为socket.io或WebSocket规范的一部分? 2)我search了很多离线模式的socket.io问题,并且看到了一些特殊的处理build议来捕获离线消息。 但是如果没有这些特殊的离线检查,这是如何工

如何在node.js中添加新的行字符?

我是node.js的新手,我试图编写一个接收http请求并通过套接字转发内容的程序。 在套接字的另一端是一个寻呼系统,需要以一个新的行字符结尾的消息。 到目前为止,它工作正常,除了有一个额外的消息与内容发送: undefined 。 当我将打印机信息的内容打印到客户端浏览器时,似乎没有新的一行。 我做对了吗? sys = require("sys"), http = require("http"), url = require("url"), path = require("path"), net = require("net"); socket = new net.Socket(); socket.connect(4000, '192.168.0.105'); var httpServer = http.createServer(function(request, response) { var uri = String(url.parse(request.url).query); var message = uri.split("="); var page = 'FPG,101,0,3!A' + message[0] + '\n'; response.writeHead(200, {"Content-Type":"text/html"}); response.write('sending message: ' […]

为什么NodeJS KeepAlive似乎没有按预期工作?

从TCP keepalive引用HowTo : 为了了解什么是TCP keepalive(我们将其称为keepalive),您只需要读取名称即可:keep TCP alive。 这意味着您将能够检查连接的套接字(也称为TCP套接字),并确定连接是否仍在运行或者是否已损坏 。 那么为什么下面的代码不会在互联网连接断开的时候抛出一些东西 ? var tls = require('tls'); var socket = tls.connect(443, "google.com", function connected() { console.log('connected'); }); socket.setNoDelay(true); socket.setKeepAlive(true, 0); socket.setTimeout(0, function(){ console.log('timeout'); }); socket.on('data', function(data) { console.log(data); }); socket.on('close', function() { console.error("close"); }); socket.on('error', function(err) { console.error("error", err); }); 使用NodeJS v0.10.17在MacOS / Debian上进行testing

socket.on事件每次响应都会重复一次以上

我已经设置socket.emit事件成功cooment。 但是,当我绑定sockets.on事件发生问题。 它被解雇多次。 $(document).on('click', '#comment_button', function() { $.ajax({ // upon success success: function (response) { if (response) { socket.emit('postcomment'); socket.on('refresh', function () { console.log('refresh'); //This console.log('refresh') is coming multiple times. I mean its occurance increases with every successful ajax response. }); } }); }); 这是我的server.js socket.on('postcomment', function () { io.sockets.emit("refresh"); }); 我已经validation,在我的server.js socket.on函数只被调用一次。 我不确定在ajax中socket.on('refresh', function […]