由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - node.js: how to post date (from backend socket) to the web
相关主题
Nodejs socket.io emit看不懂socket 会丢失数据吗?
求解:socket client用node.js, socket server用c++问个matlab socket问题
c++ question把node.js web server放在host pc, 控制别的target system
Node.js 问题请教Scala in L
大家node都是跑在nginx后面吗?consistent hashing实际应用
有没有好的node web socket框架?How to update button status from node.js server ?
一个进程如何同时做Client和Server?Node之父改行golang了
问个linux socket programming的问题node 求算法
相关话题的讨论汇总
话题: socket话题: server话题: function话题: data话题: node
进入Programming版参与讨论
1 (共1页)
w*s
发帖数: 7227
1
Note, this is not socket.io between node server and web page client.
Instead, there's a socket between node server and c program running in the
same pc.
What i want it, every time c program send node something through the socket,
i want to publish it to the web.
No matter how i tried, cannot make it work yet, certainly my node skill is
low. Can someone help me out again ? Thank you !
The socket server in the node code is copied from
var net = require('net');
//
// Server
//
var server = new net.Server();
server.on('connection', function(socket) {
console.log('Socket is open!');
socket.setEncoding('utf8');
socket.on('data', function(data) {
console.log('Received:', data);
});
socket.on('error', function() {
console.log('Socket error!');
});
});
server.listen(3e3, function() {
console.log('Server is listening!');
});
--------------------------------------------------------------
there's a socket.on('data'), so i added 1 line
res.end(data)
certainly i tried to add res into the arguments.
it complains about
Cannot call method 'end' of undefined
at Object.onConnect
at Socket.emit
at Object.afterConnect
n*****t
发帖数: 22014
2
说得很明白,res undefined,你这 res 哪来的?

socket,

【在 w*s 的大作中提到】
: Note, this is not socket.io between node server and web page client.
: Instead, there's a socket between node server and c program running in the
: same pc.
: What i want it, every time c program send node something through the socket,
: i want to publish it to the web.
: No matter how i tried, cannot make it work yet, certainly my node skill is
: low. Can someone help me out again ? Thank you !
: The socket server in the node code is copied from
: var net = require('net');
: //

w*s
发帖数: 7227
3
good to see you come back, :)
the basic idea of call flow is like this,
1. app.listen( 3000, function(req, res) {
openSocketServer(res);
}
);
2. function openSocketServer(res) { // i added res myself
server = new net.Server();
// this is pseudo code
server.on("connection', function(socket, res) {
socket.on('data', function(data) {
res.end(data);
...
}
}
my actually code is using socket client in the node server,
i was posting with socket server trying to simplify it, maybe not ...
basically how to use res inside socket.on('data') pls ?

【在 n*****t 的大作中提到】
: 说得很明白,res undefined,你这 res 哪来的?
:
: socket,

n*****t
发帖数: 22014
4
基本上,把 socket 收到的 data 存储起来,然后在 request 里送给 client。

【在 w*s 的大作中提到】
: good to see you come back, :)
: the basic idea of call flow is like this,
: 1. app.listen( 3000, function(req, res) {
: openSocketServer(res);
: }
: );
: 2. function openSocketServer(res) { // i added res myself
: server = new net.Server();
: // this is pseudo code
: server.on("connection', function(socket, res) {

w*s
发帖数: 7227
5
没看懂,不就是我想做的?有实例吗?

【在 n*****t 的大作中提到】
: 基本上,把 socket 收到的 data 存储起来,然后在 request 里送给 client。
c*********e
发帖数: 16335
6
socket.io 網頁上有例子。

socket,

【在 w*s 的大作中提到】
: Note, this is not socket.io between node server and web page client.
: Instead, there's a socket between node server and c program running in the
: same pc.
: What i want it, every time c program send node something through the socket,
: i want to publish it to the web.
: No matter how i tried, cannot make it work yet, certainly my node skill is
: low. Can someone help me out again ? Thank you !
: The socket server in the node code is copied from
: var net = require('net');
: //

w*s
发帖数: 7227
7
backend c program <-- socket --> node <--> webpage
c program send "hello",
webpage should display "hello"
如果这情况node 跟webpage之间一定还要个socket.io的话,老子无语了

【在 c*********e 的大作中提到】
: socket.io 網頁上有例子。
:
: socket,

h**j
发帖数: 2033
8
你把node混同为browser了

【在 w*s 的大作中提到】
: backend c program <-- socket --> node <--> webpage
: c program send "hello",
: webpage should display "hello"
: 如果这情况node 跟webpage之间一定还要个socket.io的话,老子无语了

n*****t
发帖数: 22014
9
没有 socket.io,server 往哪送啊?总得有个 request 吧?
我觉得你这个就类似股票行情, c program 负责更新数据,用户刷网页读成交价。不
刷网页也行,就是 socket.io 长连着。

【在 w*s 的大作中提到】
: backend c program <-- socket --> node <--> webpage
: c program send "hello",
: webpage should display "hello"
: 如果这情况node 跟webpage之间一定还要个socket.io的话,老子无语了

w*s
发帖数: 7227
10
this is how i get confused, node server uses
res.sendfile, res.write, res.end ...
to update the webpage already, why it cannot be applied to my case ?

【在 n*****t 的大作中提到】
: 没有 socket.io,server 往哪送啊?总得有个 request 吧?
: 我觉得你这个就类似股票行情, c program 负责更新数据,用户刷网页读成交价。不
: 刷网页也行,就是 socket.io 长连着。

相关主题
有没有好的node web socket框架?socket 会丢失数据吗?
一个进程如何同时做Client和Server?问个matlab socket问题
问个linux socket programming的问题把node.js web server放在host pc, 控制别的target system
进入Programming版参与讨论
w*s
发帖数: 7227
11
this is how i get confused, node server uses
res.sendfile, res.write, res.end ...
to update the webpage already, why it cannot be applied to my case ?

【在 h**j 的大作中提到】
: 你把node混同为browser了
b***e
发帖数: 1419
12
This is not a socket problem. Seems more like to be a message queue being
needed. Set up a redis, publish in c and listen in node.

socket,

【在 w*s 的大作中提到】
: Note, this is not socket.io between node server and web page client.
: Instead, there's a socket between node server and c program running in the
: same pc.
: What i want it, every time c program send node something through the socket,
: i want to publish it to the web.
: No matter how i tried, cannot make it work yet, certainly my node skill is
: low. Can someone help me out again ? Thank you !
: The socket server in the node code is copied from
: var net = require('net');
: //

n*****t
发帖数: 22014
13
哥,你读一下 express 的文档吧,res 不是凭空变出来的,得现有 client request,
你不能对着空气 send 啊。
或者你把 code 贴全一点,我告诉你问题在哪。

【在 w*s 的大作中提到】
: this is how i get confused, node server uses
: res.sendfile, res.write, res.end ...
: to update the webpage already, why it cannot be applied to my case ?

k***5
发帖数: 583
14
他默认有res在
HTTP是single transaction 的,一次基本就是一个request加一个response。
他的需要最好是用websocket。

【在 n*****t 的大作中提到】
: 哥,你读一下 express 的文档吧,res 不是凭空变出来的,得现有 client request,
: 你不能对着空气 send 啊。
: 或者你把 code 贴全一点,我告诉你问题在哪。

n*****t
发帖数: 22014
15
openSocketServer 里的 res 跟 on connect 里的 res 完全是两码事啊,即使要这么
用,on 里的 callback 去掉 res 入口参数。
首先我觉得不能让 web client 挂在那里等,这种最好做 async,其他问题是枝节。

【在 k***5 的大作中提到】
: 他默认有res在
: HTTP是single transaction 的,一次基本就是一个request加一个response。
: 他的需要最好是用websocket。

w*s
发帖数: 7227
16
i'm posting my code here,
1st is the socket server, sends data to 2nd node web server,
node web server sends data to browser using socket.io.
this is barely working.
but my question is,
how to do it without socket.io.
hang on, posting code now
the last code, i have the question
socket.on('data', function(data) {
console.log('Received:', data);
// question ???
//res.end(data); //how to make this line to work without using io.
emit
// also why output in browser is not characters but ascii numbers
io.emit('time', { time: data.toJSON() });
});

【在 n*****t 的大作中提到】
: openSocketServer 里的 res 跟 on connect 里的 res 完全是两码事啊,即使要这么
: 用,on 里的 callback 去掉 res 入口参数。
: 首先我觉得不能让 web client 挂在那里等,这种最好做 async,其他问题是枝节。

w*s
发帖数: 7227
17
// node socket server simulating backend c socket server program
//
// the task is: each time this socket server sends info to node web server
// node should send to the browser
var net = require('net');
//
// Server
//
var server = new net.Server();
server.on('connection', function(socket) {
console.log('Socket is open!');
socket.setEncoding('utf8');
socket.on('data', function(data) {
var msg = "got it";
console.log('Received:', data);
socket.write(msg, function() {
console.log('Sent:', msg);
});
});
socket.on('error', function() {
console.log('Socket error!');
});
});
server.listen(7777, function() {
console.log('Server is listening!');
});
w*s
发帖数: 7227
18










    w*s
    发帖数: 7227
    19
    // node web server
    var net = require('net');
    var http = require('http'),
    fs = require('fs'),
    // NEVER use a Sync function except at start-up!
    index = fs.readFileSync(__dirname + '/socketio.html');
    // Send index.html to all requests
    var app = http.createServer(function(req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.end(index);
    });
    // Socket.io server listens to our app
    var io = require('socket.io')(app);
    //var io = require('socket.io').listen(app);
    // Send current time to all connected clients
    function sendTime() {
    io.emit('time', { time: new Date().toJSON() });
    }
    // Send current time every 10 secs
    //setInterval(sendTime, 10000);
    // Emit welcome message on connection
    io.on('connection', function(socket) {
    // Use socket to communicate with this particular client only, sending
    it it's own id
    socket.emit('welcome', { message: 'Welcome!', id: socket.id });
    socket.on('i am client', console.log);
    });
    /////////////////////////////////////////////////
    function openSocket() {
    var socket = net.connect(7777);
    socket.setKeepAlive(true);
    socket.on('connect', onConnect.bind({}, socket));
    socket.on('error', onError.bind({}, socket));
    }
    var interval;
    function onConnect(socket) {
    var myData;
    console.log('Socket is open!');
    interval = setInterval(function() {
    var msg = parseInt(+new Date) + '';
    socket.write(msg, function() {
    console.log('Sent:', msg);
    //res.send(msg);
    });
    }, 2000);
    socket.on('data', function(data) {
    console.log('Received:', data);
    // question ???
    //res.end(data); //how to make this line to work without using io.
    emit
    // also why output in browser is not characters but ascii numbers
    io.emit('time', { time: data.toJSON() });
    });
    }
    function onError(socket) {
    console.log('Socket error!');
    // Kill socket
    clearInterval(interval);
    socket.destroy();
    socket.unref();
    // Re-open socket
    setTimeout(openSocket, 1e3);
    }
    openSocket();
    app.listen(7778);
    n*****t
    发帖数: 22014
    20
    1、node 应该把收到的 data 存储起来,有 browser 来 request 的时候 res.send
    2、如果一定要实时,socket.io 是最好的办法
    3、如果一定要 request 来了之后再送 data,应该起一个 socket server 在外面,
    node 通过 socket client 请求数据再发给 browser
    4、request 来了之后再起 socket server 会有问题,res.end 之后这个 server 就消
    失了,远端需要不断 retry connect。实在要这么做也不是不可以,你之前的 code,
    open server 传递 res,on connect 的调用参数表去掉 res,即如下:
    // this is pseudo code
    server.on("connection', function(socket) {
    socket.on('data', function(data) {
    res.end(data);
    抱歉,用 pad,不方便贴 code

    【在 w*s 的大作中提到】
    : i'm posting my code here,
    : 1st is the socket server, sends data to 2nd node web server,
    : node web server sends data to browser using socket.io.
    : this is barely working.
    : but my question is,
    : how to do it without socket.io.
    : hang on, posting code now
    : the last code, i have the question
    : socket.on('data', function(data) {
    : console.log('Received:', data);

    k***5
    发帖数: 583
    21
    比较笨的不用socket.io的方法就是browser起个timer,5s送个请求,node buffer
    data,收到browser的request时post data。这个笨法子的坏处在如果多个client的话
    ,server会overload。
    w*s
    发帖数: 7227
    22
    一定要实时,一定要从node web server把status push到browser。
    如果非要socket.io,那就这样吧。
    非常谢谢!

    【在 n*****t 的大作中提到】
    : 1、node 应该把收到的 data 存储起来,有 browser 来 request 的时候 res.send
    : 2、如果一定要实时,socket.io 是最好的办法
    : 3、如果一定要 request 来了之后再送 data,应该起一个 socket server 在外面,
    : node 通过 socket client 请求数据再发给 browser
    : 4、request 来了之后再起 socket server 会有问题,res.end 之后这个 server 就消
    : 失了,远端需要不断 retry connect。实在要这么做也不是不可以,你之前的 code,
    : open server 传递 res,on connect 的调用参数表去掉 res,即如下:
    : // this is pseudo code
    : server.on("connection', function(socket) {
    : socket.on('data', function(data) {

    1 (共1页)
    进入Programming版参与讨论
    相关主题
    node 求算法大家node都是跑在nginx后面吗?
    Contiue with my node questions有没有好的node web socket框架?
    Help C++ Template function link error .一个进程如何同时做Client和Server?
    A simple question on Flex问个linux socket programming的问题
    Nodejs socket.io emit看不懂socket 会丢失数据吗?
    求解:socket client用node.js, socket server用c++问个matlab socket问题
    c++ question把node.js web server放在host pc, 控制别的target system
    Node.js 问题请教Scala in L
    相关话题的讨论汇总
    话题: socket话题: server话题: function话题: data话题: node