new socket.io 1.0 client trying to connect on a wrong port - socket.io-1.0

when trying to upgrade my website and (nodejs + express + socket.io server) from socket.io 0.9 to 1.0, immediatelly ran into a problem: socket.io client is trying to connect not on the prort where socket.io server is listening, but on the port where the webpage which is using the client is running on.
needless to say - everything is running FOR YEARS on socket.io 0.9 with no problems...
so, specifically (everything is running on localhost):
-- node.js server:
var app = express();
var svr = http.Server(app);
require('socket.io').listen(svr);
svr.listen(3000);
-- index.html (serving on port 80 by another node.js process):
<script src="https://cdn.socket.io/socket.io-1.0.0.js"></script>
<script>
var socket = io('http://localhost:3000');
socket.on('test', function (data) {
console.log(data);
});
</script>
-- when i run this web page in chrome, i see error message in chrome js console (the message repeats every second or so):
**GET http://localhost:80/socket.io/?EIO=2&transport=polling&t=1401472842589-6 404 (Not Found) socket.io.js?_dc=1401472822276:2604**
as you can see, socket.io client library is trying to connect to the socket server on the same port, as the web page is running on - port 80 - instead of using websocket port - port 3000.
again, when i run identical code using socket.io version 0.9 - everything works.
any help?

You are configuring wrong way, when you are using express then for a now time syntax has been changed for newer version for socket.io 1.0. same syntax like express middleware.
Please user.
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
server.listen(3000);
WRONG PART in code
require('socket.io').listen(svr);

Related

HTTP request to 127.0.0.1:80 fails

Environment
Android Studio latest release
Android Emulator running Android Nougat
MAMP to serve as HTTP server
Problem
My simple method
Future<String> _getSimpleReply( String command, callback, errorCallback ) async {
try {
HttpClientRequest request = await _myClient.get( '127.0.0.1', 80 '/' );
HttpClientResponse response = await request.close();
await callback( response.toString() );
} on SocketException catch( e ) {
errorCallback( e.toString() );
}
}
fails with an error:
cbLoginFailed :: SocketException: OS Error:
Connection refused, errno = 111, address = 127.0.0.1, port = 46414
Currently using the Android emulator, which has been configured for internet access:
<uses-permission android:name="android.permission.INTERNET"/>
For test purpose, the HTTP server is MAMP, which executes the index.php script and returns some simple string, if I enter this link in a browser:
http://127.0.0.1/index.php
The situation does not change, if I replace 127.0.0.1 with localhost.
But if I do change 127.0.0.1 with a valid public FQDN, the method works just fine:
doLoginSuccess :: Instance of '_HttpClientResponse'
Question
Why does the android emulator / flutter / dart handles 127.0.0.1 different than a
valid FQDN?
And how to I change this behaviour?
As pointed out by the first comment, localhost or 127.0.0.1 is - when running an app on Android emulator - in fact the local machine on which the Android app is executing. With respect of the Android app, the local machine is the emulated Android system.
Therefore, the http request from the flutter code doesn't leave the emulator at all. Since the Flutter app doesn't provide a HTTP Server, the emulator refuses the http request.
Need to change to the address of the host machine, on which the emulator is executing.
The Android emulator uses special IP addresses to communicate with its host or the host gateway.
Habe a look in the Android documentation.
E.g., the host‘s loopback Interface is identified by this IP: 10.0.2.2
So, if normally one would use 127.0.0.1:80, one can substitute it with 10.0.2.2:80.
You are trying to access localhost which is your phone/emulator not your PC which I assume is running your test server.
You need to create a network between your PC and phone and use the local IP address instead

How to access the local machine's serial port data using a Heroku application

I have an existing Ruby on Rails Heroku application. I want to get the serial port data from the COM 1 port in a Windows computer using this application, but since Heroku is a cloud-based platform running on a Linux server, I am unable to get serial port data from the local machine where the Heroku application is running. If I run the standalone Ruby code on that Windows machine, then it works fine and I am able to get my desired data.
I am getting the following error
Unable to open COM1
How do I solve this issue?
The part of code of my Rails application through which I am accessing serial port data is:
port_str = 'COM1' #may be different for you
baud_rate = 2400
data_bits = 8
stop_bits = 1
parity = SerialPort::NONE
sp = SerialPort.new(port_str, baud_rate, data_bits, stop_bits, parity)
i=sp.gets.chomp
puts i
This code is working fine if I run it on that local machine. 'serialport' gem is there on my gemfile.
Rails is a web server technology that runs on a web server. It builds HTML pages that get sent to a client computer and are rendered by the browser.
When you run Rails locally you are mimicing a real web server - localhost is basically running a web server on your local machine. That is why you can cheat and use Ruby code in your Rails app that locally can access the port of your local machine, but once you run your Rails app on a real web server (like in Heroku) you cannot do this, so you have the wrong tool for the job you are trying to do.
Not only that but since Rails is a web technology you have a web application that runs inside the browser and you cannot easily access the port on a client machine from a web browser. More information on that is in "How to read serial port data from JavaScript".
The ONLY reason your Ruby code is able to access the port is because it is not running in the browser when you run on localhost but it is running inside the web server that gets fired up on localhost, so when the app runs on a real web server that Ruby code will try to access the port of the server not any client machine.

Xamarin VS2015 ios app connect to web app api

I have a iOS app that I am building in Visual Studio 2015 with Xamarin. I also have a Web Application with an Api, no authorisation. But when I try to connect to the api I get an error saying "Connection Refused".
I call the api through the following code:
var client = new HttpClient();
var data = await client.GetAsync("http://localhost:7074/api/values/get");
Config your Web API with an IP Address, say local 192.168.x.x
And replace localhost by that IP
var data = await client.GetAsync("http://191.268.x.x:7074/api/values/get");
localhost will point to your iOS device/emulator, not the server. You need to specify the IP or FQDN in the url. Also be be sure the firewall is open and that IIS is setup to accept remote connections.
localhost is your iPhone / iPad or simulator (the device where you run your app). You have to change it to the IP/Domain of your server.
If the server is your dev machine, ensure, that your phone is connected to the same network (e.g. via WiFi), else you might have the same problem, because your dev machine might not be reachable.
You can find the IP of your dev machine by typing ipconfig | find "IPv4" into the console (on Windows).

Connect Nodejs Faye-Client to Rails Faye server

I have a Faye server running under Rails (with redis backend) and would like to allow a nodejs script to communicate with my clients via Faye. Therefore I need to connect my Nodejs Faye client to the Rails process, that runs Faye.
Using:
var client = new faye.Client('http://localhost:6379/faye');
//I also tried ports 3000 and 8000 and verified that redis runs at 6379
faye.Logging.logLevel = 'debug'
faye.logger = function(msg){
console.log(msg)
}
client.connect();
I only see:
[Faye.Client] Initiating handshake with "undefined//undefinedundefined"
[Faye.Dispatcher] Selected
"long-polling" transport for "http://localhost:6379/faye"
[Faye.Client] Passing through "outgoing" extensions:
{"channel":"/meta/handshake","version":"1.0","supportedConnectionTypes":["in-process","websocket","long-polling"],"id":"1"}
Do I really need to start a nodejs-faye-server as well to configure it to use redis, or shouldn't it be enough to have NodeJS acting as a client to Rails? Unfortunately this answer did not help me. Has anybody ever done this? If so, how?
Found the answer. Turned out the problem was not in the NodeJS script, but in the way I started in from Ruby. Turns out that system waits for value to be returned and thus does not process the Faye-message. Using spawn() solved the issue.

Serving Juggernaut 2 over pure HTTPS connection

I have a Ruby on Rails website at which I force all connections to be SSL. I need all connections from that site to use HTTPS as well. Also, Google Chrome will automatically switch to HTTPS even if I connect to another port.
This means that I cannot connect to
http://www.mysite.com:8080
I have to serve the juggernaut js file over https. But that doesn't work since Juggernaut doesn't want to use https instead of http at its internal webserver. So I copied the application.js file from the juggernaut folder /usr/local/lib/node_modules/juggernaut/public/application.js into my rails folder public/juggernaut and changed the following line in my HTML code:
to
Now I seem to be able to at least initiate a Juggernaut object. The problem arises when I start to actually do some listening. I get this error:
Not found: https://www.mysite.com:8080/socket.io/1/?t=1340749304426&jsonp=0
So either I need to
a) be able to change it so I can actually have Juggernauts webserver use https instead of http. This is preferable.
or
b1) fix Juggernaut so it doesn't try to access socket.io over port 8080 and
b2) add socket.io to my server, preferably under the www.mysite.com/juggernaut folder instead of the root.
Any ideas?
Thanks!
Might be a little late but I was able to get it to work using this. (My juggernaut is hosted on heroku)
var jug = new Juggernaut({
secure: true,
host: 'yourHostHere',
port: 443,
transports: ['xhr-polling','jsonp-polling']
});

Resources