gpt4 book ai didi

node.js - 在node.js中打开套接字时发送数据

转载 作者:太空宇宙 更新时间:2023-11-03 23:10:52 24 4
gpt4 key购买 nike

我正在配置一个 Nodejs 服务器,以便将其用作推送通知的服务器。

我有这个代码:

io.sockets.on('connection', function(socket) {
console.log(socket);
// watching the xml file
fs.watchFile('client.xml', function(curr, prev) {
// on file change we can read the new xml
fs.readFile('client.xml', function(err, data) {
if (err)
throw err;
// parsing the new xml datas and converting them into json file
parser.parseString(data);
});
});
// when the parser ends the parsing we are ready to send the new data to the
// frontend page
parser.addListener('end', function(result) {
socket.volatile.emit('notification', result);
});
});

在服务器上。在客户端上:

<script>
function test(data) {
console.log(data);
jQuery('#' + data.id).html(data.content);
}
</script>

<script>
// creating a new websocket
var socket = io.connect('http://10.0.0.113:8000');
// on every message recived we print the new datas inside the #container div
socket.emit('data', {id : 'id'});
socket.set('nickname', {id : 'test'});
socket.on('notification', function(data) {
_efbn(data.callback, window, data.response, data);
});

/**
* Función que ejecuta una función por nombre. Puede usar namespaces
* (algo.algo.algo.funct)
*
* @see http://stackoverflow.com/questions/359788/javascript-function-name-as-a-string/359910#359910
*/
function _efbn(functionName, context) {
var args = Array.prototype.slice.call(arguments);
args = [ args[2], args[3] ]; // Fix para IE.
var namespaces = functionName.split(".");
var func = namespaces.pop();
for ( var i = 0; i < namespaces.length; i++) {
context = context[namespaces[i]];
}
try {
if (typeof context[func] == 'function') {
return context[func].apply(this, args);
}
} catch (e) {
console.log(e);
}
return null;
}
</script>

一切都如我所愿。但是,我想发送如下内容:

socket.set('site', '12345');
socket.set('object', 'ABCDE');

为了确定我应该监听哪个 xml,而不是一直监听 client.xml。

我该怎么做?我在 server.js 上有这个:

id = require('url').parse(req.url, true).query.id;

但是,由于服务器仅在连接打开时执行,而在套接字打开时不执行,因此如果客户端和服务器之间的连接失败,当jquery重试打开套接字时,id将为空...

最佳答案

我会在服务器上执行此操作,

io.sockets.on('connection', function(socket) {
console.log(socket);
socket.on('setup', function(config) {
// use your connection specific config variables like
var id = config.id; // and then use id in your logic below.
// watching the xml file
fs.watchFile('client.xml', function(curr, prev) {
// on file change we can read the new xml
fs.readFile('client.xml', function(err, data) {
if (err)
throw err;
// parsing the new xml datas and converting them into json file
parser.parseString(data);
});
});
// when the parser ends the parsing we are ready to send the new data to the
// frontend page
parser.addListener('end', function(result) {
socket.volatile.emit('notification', result);
});
});
});

在客户端,

// creating a new websocket
var socket = io.connect('http://10.0.0.113:8000');
socket.on("connect", function() {
// Setup your connection on the server-side by providing it
// some config variables.
var config = {
id: "id",
nickname: "test"
};
socket.emit("setup", config);
// on every message recived we print the new datas inside the #container div
socket.on('notification', function(data) {
_efbn(data.callback, window, data.response, data);
});
});

这样,每当建立新连接时,都会首先使用所需的配置变量在服务器上调用 setup 事件。

关于node.js - 在node.js中打开套接字时发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12044543/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com