I tried to connect python client to node.js server, and experience HTTP 400 errors.
node.js server code:
var socket = require('socket.io');
var express = require('express')
, http = require('http');
var app = express();
app.set('port', process.env.PORT || 8080);
var server = http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
var io = socket.listen(server);
io.sockets.on('connection', function () {
console.log('hello world im a socket');
});
python client code:
from socketIO_client import SocketIO
def on_response(*args):
print 'on_response', args
import logging;
logging.basicConfig(level=logging.DEBUG)
socketIO = SocketIO('localhost', 8080)
socketIO.on('news', on_response)
socketIO.wait(seconds=1)
when i run client.py after starting the server getting the following error:
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): localhost
DEBUG:requests.packages.urllib3.connectionpool:"GET /socket.io/1/ HTTP/1.1" 400 None
If I create a javascript client, then it works well. Could someone help me resolve this issue please?
Related
I am getting this error:
WebSocket connection to 'ws://localhost:8000/socket.io/?EIO=3&transport=websocket' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
I have looked for this error but it seems for me that my configuration of the sockets is well and I do not think is for the warning of electron.
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var JSONTCPSOCKET = require('json-tcp-socket');
var JSONTCPSOCKET = new JSONTCPSOCKET({tls: false});
require("./rabbit")(io, JSONTCPSOCKET);
var socket = io('http://localhost:8000',{transports: ['websocket',
'flashsocket', 'htmlfile', 'xhr-polling', 'jsonp-polling', 'polling']});
Any idea?
Thanks mates!!
It was a silly mistake.
The file that contains the server that listen in that port is in my case server.js.
When you run it with node, the start file is server.js but when you run it with electron the start file is main.js and I was never running server.js when I executed with electron, so I was not listening in that port.
I have a Socket.IO server (Node.js, NPM) that works fine, but the problem now is that it only works in LAN.
I'm using Socket.IO 2.1.1.
My IP is my IPv4 address, and the port is 80. Here is my server.js code.
var express = require('express');
//var http = require('http');
var url = require('url');
var socket = require('socket.io');
var crypto = require('crypto');
var fs = require('fs');
//App setup
var app = express();
var server = app.listen(80, function() {
console.log('listening to request on port 80! Yay!');
});
//Static files
app.use(express.static('public'));
io.on('connection', function(socket) {
//stuff to do on connection
}
and for the client
var socket = io.connect('http://192.168.254.7:80', {transports:['websocket']});
and I also included
<script language="javascript" src="http://<ipv4>:80/socket.io/socket.io.js"></script>
where IPv4 is the IP address i'm using.
How do I make it public? (not only locally / LAN) I've tried changing
io.connect('http://192.168.254.7:80', {transports:['websocket']});
to
io.connect();
I've even changed the port from 3000 to 8080, and to 80.
I let my friends try to access the website, and it always shows
net::ERR_CONNECTION_TIMED_OUT. Does it even load to the other's device?
Again, how do I make this public? Thanks.
I am trying to run a node js app on Heroku using WebSockets. However, I am not able to resolve this error (As seen in conosle of Chrome browser)
WebSocket connection to 'wss://myappname.herokuapp.com:27225/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
I am using 'wss' since Heroku runs on HTTPS.
My client side code is :
$.get("https://myappname.herokuapp.com/port",function(data){
port = data;
console.log(data);
host = 'wss://myappname.herokuapp.com:' + port + '/';
ws = new WebSocket(host);
});
My server side code is :
var WebSocketServer = require("ws").Server
var fs = require('fs');
var express = require('express');
var app = express();
var http = require('http');
var port = process.env.PORT || 5000;
var request = require('request');
var server = http.createServer(app);
var serverOnPort = server.listen(port);
console.log("Server listening on port ",port);
var wss = new WebSocketServer({server: serverOnPort});
console.log("websocket server created");
Any help would be appreciated.
Thank You.
So it seems like I was trying to over-do it with the port number. Just using the host as wss://myappname.herokuapp.com/ works well.
I also found this problem. It seems Heroku will automatically route port number. It does's allow to specify port number in url. In my chrome, it show ERR_CONNECTION_RESET. This also happen with XMLHttpRequest. Port number still need when you test with localhost or another server which is not Heroku.
I referred to the previously asked question on this but couldnt resolve it. I have Express server installed and trying to run Index.html file through it.
But I am getting 'Cannot GET /' as the response.
Here is the server.js through which I calling the index.html
var express = require('express');
var app = express();
app.get('index.html', function (req, res) {
app.use("/", express.static(__dirname));
});
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
Thanks in advance!!
When you access a directory on your hosted site, say the root directory of localhost on port 8080, using http://localhost:8080/ for URL, the browser does not send a request for 'index.html` to the server and just uses whatever the server sends back.
It's the express static middleware which in response to a browser request with no filename will check if the folder exists and (by default) return any index.html contained in the folder. So your line of code for routing, app.get('index.html') never executes, and the browser gives you the error message.
Here's a mini static express server if you want to try it.
var express = require('express');
var app = express();
app.use(express.static('../public')); // path to your public directory
var server = app.listen(8080, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
If you want a simple static, folder-as-server kind of thing, you can do it without express way like "/public":
var fs = require("fs");
var host = "localhost";
var port = 8000;
var express = require("express");
var app = express();
app.use('/', express.static(__dirname));
app.listen(port, host);
I put this in the file express.js, so that is in the same folder than index.html (even associated .js with node.exe). This way the folder is the root of the server.
I have the following server.js running:
module.exports = server;
var express = require('express');
var fs = require('fs');
var server = express.createServer();
var port = 58000;
server.listen(port);
var io = require('socket.io').listen(server);
server.use(express.static('/', __dirname + '/../public'));
server.use(express.logger());
io.on('connection', function(client){
console.log('new client connected ' + client);
client.on('message', function(){
console.log('client wants something');
});
});
Simple express.static server for files in a /public subfolder, plus socket.io functionality. With this setup, any request for the 'socket.io.js' file fails, i.e.
http://localhost:58000/socket.io/socket.io.js
returns a 404 error (file not found). Static file server works correctly. If I simply use the 'http' module instead of 'express' (commenting out express.static and express.logger lines) socket.io.js is served correctly. How can I combine both functionalities?
Express 3.0.0 (lastest) change its API.
Here is a question very similar to yours that delivers the response.
var express = require('express')
, http = require('http');
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);
...
server.listen(8000);
Make sure you have the last versions of express.js and of socket.io.js.
My side it's working great with
express#2.5.8
socket.io#0.8.5
node#0.6.5
Otherwise, a solution can be to call var io = require('socket.io').listen(server); after your server.use