Whenever I execute my code, I get message at console that it's connected than after that that nothing happens.
Here is the code:
server-side
var app = require('express')()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server);
server.listen(80);
app.get('/', function (req, res) {
res.sendfile('index.html');
});
io.sockets.on('connection', function (socket) {
console.log('m connected');
socket.emit('news', { hello: 'm joooon ' });
socket.on('my other event', function (data) {
console.log(data);
});
});
client-side
<!DOCTYPE html>
<html>
<head>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
</head>
<body>
<div id="lo">magudi</div>
</body></html>
On the client side, your not listening to the port.
var socket = io.connect('http://localhost:80');
Related
I have tried many methods to connect socket-io client and server with express but its not working and now i do not know what could be error.
i have tried to clone example available on socket-io website still it does not show any connectivity or any functions do not emmit.
This is server.js and this is my code i have made many changes in it but not working.
var express = require('express');
var app = express();
var path = require('path');
//require socket.io
var http = require('http').createServer(app);
var io = require('socket.io')(http);
//Setup a public path to load files
app.use(express.static('public'))
//app.use(express.static(path.join(__dirname, 'public')));
//Route for index
app.get(('/'),(req,res)=>{
res.send('index');
});
//Socket connection
io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
app.listen(3000,()=>{
console.log('server running on port 3000');}
);
This is index.html that the server.js is sending i only included the script
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:3000');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
I expect it to console log when socket is connected and disconnected and emit the functions normally.
Hello Here is your working example of socket.io
In Your server code
const express = require('express')
const app = express();
//Listen on port 3000
server = app.listen(3000, () => {
console.log('server running on port 3000');
})
//socket.io instantiation
const io = require("socket.io")(server)
//listen on every connection
io.on('connection', (socket) => {
console.log('New user connected')
socket.emit('news', { hello: 'world' });
})
Here is HTML code
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:3000');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
Good day, I'm trying to use socket.io i facing some problem with 404 not found. I'm using :84 as my xampp port
Folder structure
--tests
--node_modules
--app.js
--index.html
Here is my html content
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.7.3/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:84/test/');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
and this app.js
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
app.listen(8080);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
I'm bit confuse about setting this part
var socket = io.connect('http://localhost:84/test/');
and this part
app.listen(8080);
I'm using windows 7 64 bit and i'm using xampp to run it. When i run it i get 404 error like this
"NetworkError: 404 Not Found - http://localhost:84/socket.io/?EIO=3&transport=polling&t=Ll8amII"
how can i fix it? sorry for my english and thanks in advance.
You are connecting to the wrong port.
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io('http://localhost:8080'); // CHANGE HERE
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', {my: 'data' });
});
</script>
XAMPP has nothing to do with express.
i use code from examples of socket.io site and has some problem
My server code (on debian 192.168.5.200)
var app = require('express')()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server);
server.listen(1337);
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
My client code (index.html)
<script src="http://{host ip}:1337/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://{host ip}:1337');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
i start node server
open in browser http://{host ip}:1337
and... got 404 on socket.io connection
it try to get "/api/1/?t=..." url and got answer by express "Cannot get /api/1/?t=..." with 404 error
Please help me (
Try to change your client code to:
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect();
socket.on("connect", function() {
socket.on('news', function (data) {
socket.emit('my other event', { my: 'data' });
});
});
</script>
Also, in order to be able to go to /api/1... you need to register the corresponding app.get, e.g. as app.get("/api/*", ..., which would handle all the connections to /api/.... Otherwise it is expected that you will be getting the 404 error.
Server:
var
io=require('socket.io'),
express=require('express'),
UUID=require('node-uuid'),
path=require('path'),
app = express();
app.configure(function(){
app.use(express.static(path.join(__dirname,'')));
});
var server = require('http').createServer(app).listen(8080);
sio = io.listen(server);
app.get('/', function(req, res){
res.sendfile('/index.html', {root:__dirname});
});
sio.sockets.on('connection', function(socket){
socket.userid = UUID();
socket.on('message', function(){
console.log('client just sent something');
socket.emit('news', { hello: 'world' });
});
});
Client connects index.html at first, then click a href to gameroom.html. This is gameroom.html:
<html>
<head>
<script src="/socket.io/socket.io.js"></script>
<script type="text/javascript" src="game.room.js"></script>
<script type="text/javascript" src="client.js"></script>
</head>
</html>
client.js:
window.onload = function(){
var game = new game_room();
}
game.room.js:
var game_room = function(){
this.socket = io.connect();
this.socket.on('connect', function(){
this.socket.emit('message', 'Hello server');
});
}
When the client connects, it should emit the message and the server writes the log, but I didn't get any log. I tried the code without this.socket.on('connect') in game.room.js, and the client can emit the message and I got the log. Also, if I transfer the code from game.room.js to client.js, i.e. in window.onload I created var socket = io.connect() and socket.on('connect', function() {socket.emit('messsage', 'Hello server')}); it works fine.
Can someone help me out or explain what is happening? Thanks.
You are loosing context you could try bind:
var game_room = function(){
this.socket = io.connect();
this.socket.on('connect', function(){
this.socket.emit('message', 'Hello server');
}.bind(this));
}
or you could just set this.socket to some local var:
var game_room = function(){
var socket = this.socket = io.connect();
this.socket.on('connect', function(){
socket.emit('message', 'Hello server');
});
}
Here is my server
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
Here is my client index.html
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:80');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
Here are my commaneds
node server.js
and on browser I am hitting URL localhost:80/index.html
after hitting above URL I am getting message
Welcome to socket.io.
I am using nodeJS v0.10.9
socket.io provides a socket.io server, not a web server. So index.html in the context of socket.io doesn't exist.
Instead, you could use Express to provide a basic web server, combined with socket.io to provide messaging:
// server.js
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
app.use(express.static(__dirname + '/public'));
server.listen(80);
// ./public/index.html
<!doctype html>
<html>
<head>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect(); // let socket.io autodiscover the server
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>