javascript - Can you connect to a running Node JS server from a command line js script on another computer? -
i have spent many days on , been unable solution.
i have node server running (very basic) working , have web pages can connect , work server.
but need debian based raspberry pi run js file using node can connect existing node server.
is possible or understanding of node incorrect.
this basic server
var socket = require('socket.io'); var express = require('express'); var http = require('http'); var app = express(); var server = http.createserver(app); var io = socket.listen(server); io.on('connection', function (client) { console.log('connected'); client.on('deviceevent', function (data) { io.sockets.emit('return',{ param1: data.param1, param2: data.param2, param3: data.param3 }); console.log(data); }); client.on('disconnect', function () { io.sockets.emit('user disconnected'); }); }); server.listen(3000); console.log('listening');
and how expecting able connect via cmd line js file.
(this doesnt work @ all)
var socket = require('socket.io'); mysocket = socket.connect('http://192.168.1.70:3000'); mysocket.emit('deviceevent', { param1: "update", param2: "0", param3: "1" });
so possible? have server working damned if can js file can run @ cmd line connect.
any appreceiated.
btw. socket.io examples related web page connecting server doing.
you need use socket.io-client connect socket.io server
var io = require('socket.io-client'); mysocket = io.connect('http://192.168.1.70:3000'); mysocket.on('connect', function(){ mysocket.emit('deviceevent', { param1: "update", param2: "0", param3: "1" }); });
similar question: how connect 2 node.js servers websockets?
Comments
Post a Comment