I'm trying to figure out what is wrong with my code / settings for a while now.Basically I want to send a message from the client to the server. The following works in all browsers except FF(11) and chrome(18.0.1025.152 m).
Here is some info:
Server code:
var io = require('C:/Users/shlomis/node_modules/socket.io/lib/socket.io').listen(8080);
io.sockets.on('connection', function (socket) {
socket.on('my event', function (msg) {
console.log("DATA!!!");
});
});
I could not find a way to require without a full path
Client code:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Hello World!</title>
<meta charset="utf-8">
<script src="http://localhost:8080/socket.io/socket.io.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var socket = io.connect('http://localhost:8080');
$("#button").click(function() {
socket.emit('my event' ,"Hello World!");
})
})
</script>
</head>
<body>
<button type="button" id='button'>Send Message</button>
</body>
</html>
Modules versions:
C:\Users\shlomis>npm ls
mukhin_chat@0.0.1 C:\Users\shlomis
├─┬ express@2.5.9
│ ├─┬ connect@1.8.6
│ │ └── formidable@1.0.9
│ ├── mime@1.2.4
│ ├── mkdirp@0.3.0
│ └── qs@0.4.2
└─┬ socket.io@0.9.5
├── policyfile@0.0.4
├── redis@0.6.7
└─┬ socket.io-client@0.9.5
├─┬ active-x-obfuscator@0.0.1
│ └── zeparser@0.0.5
├── uglify-js@1.2.5
├─┬ ws@0.4.12
│ ├── commander@0.5.2
│ └── options@0.0.3
└── xmlhttprequest@1.2.2
Node log:
C:\Users\shlomis>node C:\dev\wamp\www\AR\js\videoServer.js
info - socket.io started
debug - served static content /socket.io.js
debug - client authorized
info - handshake authorized 17502185141066845391
debug - setting request GET /socket.io/1/websocket/17502185141066845391
debug - set heartbeat interval for client 17502185141066845391
debug - client authorized for
debug - websocket writing 1::
debug - setting request GET /socket.io/1/xhr-polling/17502185141066845391?t=1334041653716
debug - setting poll timeout
debug - discarding transport
debug - cleared heartbeat interval for client 17502185141066845391
chrome WS request (101 Switching Protocols):
chrome XHR request:
Update : Iv'e added
socket.on('connect', function () {
console.log("connected");
});
It never fires on chrome.
So what could be wrong? please help out :)
I believe you're using the wrong socket.io.js file for the client?
https://github.com/LearnBoost/socket.io-client/tree/master/dist
you should use these files in a folder where you have your client-side stuff on, ie:
../js/socket.io.min.js
About the require problem, are you using npm? go into the nodeJS project folder and run the following:
npm install socket.io
this should install to the node_modules
folder inside your project folder and by doing a require('socket.io');
you should be able to access the module.
if this isn't working, you probably have a corrupt installation of node.
PS: Are you using the same port for the files? you seem to be looking for the socket.io file on port 8080 and then connecting to nodeJS on port 8080 for socket connections...
try using another port?