Socket IO V0.7: Where to put flashsockets SWF file? - javascript

I currently have my 'WebSocketMain.swf' file sitting in the same directory as 'socket.io.min.js' but Firefox doesn't seem to want to use flash sockets. It always reverts to XHR-Polling. See test case here : http://thebeer.co/labs/rt/test.php (page is blank, check JS console for feedback).
Is this the right place for it?
Do I need to direct Socket.io to the location of this SWF file?
UPDATE:
My node server requesting minified client js.
var $ = require('jquery');
var http = require('http'),
url = require('url'),
https = require('https'),
fs = require('fs'),
crypto = require('crypto'),
io = require('../'),
sys = require(process.binding('natives').util ? 'util' : 'sys'),
server = http.createServer(function(req, res){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end();
});
server.listen(80,"173.201.181.56");
var io = io.listen(server), buffer=[];
io.set('browser client minification', true);//<<minified client js requested here.
My client side including the minified JS:
<script src="http://173.201.181.56:60/socket.io/socket.io.js"></script>

I see that you decided to host the file your self. Did you know that Socket.IO also serves the client for you? See https://github.com/LearnBoost/Socket.IO/wiki/How-do-I-serve-the-client
You can even configure it, so it outputs a minified build: https://github.com/LearnBoost/Socket.IO/wiki/Configuring-Socket.IO
This client also knows where the location of the .swf file is, so you don't need to configure anything.
If you still want to serve the file your self (which is not recommended) You need to set the window.WEB_SOCKET_SWF_LOCATION to http://yoururlhere.com:port/socket.io/static/flashsocket/WebSocketMain.swf or WebSocketInsecure.swf (this depends if you go cross domain or port, but the bundled socket.io client handles this already for you)

Related

Issues with linking Javascript files with Node.js and HTML

I am making an application with node.js and I basically want render a canvas with node.js so I can access a json file on the server since you can't change client storage on browsers. To add on when running my files I get this weird syntax error.
Uncaught SyntaxError: Unexpected token '<'
Further examination shows that my javascript files that I am requiring (see internals folder) are being overwritten with html, thus causing the error. My question is why is it overwritting only the files I am requiring from the html I am rendering? To add on I am running on a dynamic port (65535) and on a localhost (client). My code is here
Your node server is not serving your static files, it only serves one html.
You might need to set up a static file server like or programm you server to lookup GET /internals/render.js and scaleCanvas.js
You can use express for that, it has all this out of the box.
https://expressjs.com/en/starter/static-files.html
var app, server,
express = require('express'),
path = require('path'),
host = process.env.HOST || '127.0.0.1',
port = process.env.PORT || 3000,
root = path.resolve(__dirname, '..');
app = express()
app.use(express.static(root + '/public'));
server = app.listen(port, host, serverStarted);
function serverStarted () {
console.log('Server started', host, port);
console.log('Root directory', root);
console.log('Press Ctrl+C to exit...\n');
}
This is because you are sending the index.html file for all requests in your app.js file, ignoring the request URL.
You need to properly serve the JS files. You can take a look here for more details: https://nodejs.org/en/knowledge/HTTP/servers/how-to-serve-static-files/

I can view my nodejs code in the browser. I want to prevent this

i can hit the url like
localhost/foodbucket/app.js
and see everything.
I want to prevent this.
var config = require('./config/config.js');
var app = require('express')();
var http = require('http').Server(app);
var mysql = require('mysql');
var pool = require('./config/database.js')(config.MYPOOL,mysql);
var io = require('./lib/socket.js')(http,mysql,pool);
var notificationcron = require('./crons/notification.js')
(io,pool,mysql,config.NotificationStatus);
const router = app.Router();
router.get('/', "Error 404");
http.listen(4849, function() {
console.log('Listening on port ' + 4849);
});
Solution : Add an .htaccess file inside Node directory.
Write "Deny from all" in .htaccess
To avoid having your web server share the data on a URL: Don't put the file in a directory that your web server is configured to publish over HTTP in the first place.
Then, if you need to access that file from PHP (as your previous question says you want), use either a relative directory path (one which starts ../ to go up a directory) or an absolute path (like /var/secret_node_code/app.js) to access it.
Your app.js file in the server/www path.
I suggest create on the C:// a folder and paste you project to in after that, open a CMD and navigate to your folder and start your program with node app.js.
You should start your nodeJS application in console.
Just open a console (in windows run cmd, in linux start a terminal) and go to the path, where is your nodeJS application. Enter node app.js and press enter.
After that you can see the result in the browser in url "localhost"

Locating files using Node.js and Socket.io

I am having trouble with local includes on the client side using Node.js and Socket.io. This may be to my PHP/Apache mindset I have had for file requests for most of my life.
On my server, I load the page likewise:
var express = require("express");
var app = express();
var path = require("path");
var server = require("http").createServer(app);
var io = require("socket.io")(server);
var mysql = require("mysql");
var port = process.env.PORT;
var ip = process.env.IP;
app.use(express.static(__dirname + "/client"));
//start opening socket connection handlers ...
And my files are organized likewise:
games
libraries
bigInt
threejs
etc...
version_1
client
index.html
index.js
index.css
server.js
database.sql
version_2
version_3
etc...
Depending which version I want to run, I open that version's directory and run its server.js file. The line redirects the client to /client/index.html with the line app.use(express.static(__dirname + "/client")). But now only files that are in the client folder are reachable by <script></script> or <link> tags but not those libraries in the libraries folder that I use across versions.
How do I change my code to be able to access files inside the libraries folder from /version_x/client/index.html while still directing the client to proper html file?
Note: Due to this issue, I have been forced to use only libraries with supported CDNs for the past couple weeks I have been learning Node.js.
Add the following line right after var ip = process.env.IP;:
app.use('/libraries', express.static(path.join(__dirname, '..', 'libraries'));
What this does is adding a new route to your application server. All your files inside your /games/libraries folder are now accessible via /libraries.
How does it work? Your express router uses different middlewares based on the provided paths. This line tells the router, to use the static middleware and serve files from ../libraries when a HTTP Request for anything under /libraries comes in.
You can serve more folders with express.static
//Serve Client Folder
app.use(express.static(__dirname + "/client"));
//Server External Libraries Folder
app.use('/libs', express.static(__dirname + "/../libraries"));
//Ex: <script src="libs/threejs/threejs.js">
//Will load libraries/threejs/threejs.js

Express and node.js run from a folder; all links point to url root

I have installed node inside a folder and am forwarding it through apache. I am doing this because I have several virtualhosts run through apache, and I do not want to take the time to set up everything through node.
Apache and Node.js on the Same Server
However, I am trying to create a chat engine. I try to include some js files, but they search for http://example.org/myscript.js instead of http://example.org/chat/myscript.js
I got around this by using
<base href="/chat/">
However, now I am trying to integrate socket.io. I included socket.io from https://cdn.socket.io/socket.io-1.3.5.js because I could not get the locally serverd /socket.io/socket.io.js to properly be located.
Now socket.io is trying to connect to http://example.org/socket.io
That dierectory does not exist. If anything, the proper path should be http://example.org/chat/socket.io
I have been looking all over the internet for a solution. There must be something fundamental or obvious about how nodejs/express operates that I am missing. Thanks a million!
Server.js - This is the file I start with node.
var express = require('express');
var path = require('path');
var app = express();
var http = require('http').createServer(app);
var io = require('socket.io').listen(http, { log: true, resource:'/socket.io/'});
app.use('/socket.io', express.static(path.join(__dirname,'/node_modules/socket.io/lib/')));
app.use('/bootstrap', express.static(path.join(__dirname,'/node_modules/bootstrap/dist/')));
app.use('/', express.static(__dirname));
var server = app.listen(8000);
io.sockets.on('connection', function(socket) {
console.log('A user connected!');
})

Node.js socket.io.js not found or io not defined

I'm trying to run a node.js application on my freebsd server, but I can't get the socket.io library to work with it. I've tried including:
<script src="/socket.io/socket.io.js"></script>
Which gives a 404 error, and if i link directly to the file (i.e. where it is in my public_html folder) I get the io not defined error.
Thanks in advance
Try creating another node.js application that has this single line in it and then run it with node.js
var io = require('socket.io').listen(8000);
Then in your browser visit http://127.0.0.1:8000 and you should get the friendly "Welcome to socket.io." greeting. If you are getting this then socket.io is running and will serve the socket.io.js file.
The only other thing that I can think of that might be happening is that you might not be linking to the alternate port in your client file. Unless you're running the socket.io server on express which is running on port 80. For now create a client file that has the script source for socket.io set to
<script src="http://127.0.0.1:8000/socket.io/socket.io.js"> </script>
This should connect to the socket.io server running on port 8000 and get the socket.io.js file.
Your node.js application still has to serve it - it does not get served automagically. What do you have in your server? It should be something like
var app = require('express').createServer();
var io = require('socket.io').listen(app);
or similar (the listen is important). The location is not a real location on the disk - socket.io library should intercept the URL and serve its clientside library, as far as I understand.
Add the following after body parser:
, express.static(__dirname + "/public")
So something like:
var app = module.exports = express.createServer(
express.bodyParser()
, express.static(__dirname + "/public")
);
For those who got the same kind of issue if they run (open) your html file directly from your local file directory(ex: file:///C:/Users/index.html).
Solution: You have to run(open) the file through localhost (ex: http://localhost:3000/index.html) where the server is listening to.
Below code snippet shows how to create a server and how to wire together with the express and socket.io
const express = require("express");
const app = express();
const httpServer = require("http").createServer(app);
const io = require("socket.io")(httpServer);
///////////////////////////////////////////////////////////////
// Any other server-side code goes here //
//////////////////////////////////////////////////////////////
httpServer.listen(3000, () => {
console.log(`Server listening to port 3000`);
});

Categories