Can't connect and getting a "unhandled socket.io url" error with MAMP + Socket.io (node.js)
Running socket.io version 0.8.5, I finally got this working using the following code:
server.js
var sys = require('sys'), express = require('express'), app = express.createServer('localhost'), io = require('socket.io'); app.use(express.static(__dirname + '/public'));app.get('/', function (req, res) { res.send('Hello World');});app.listen(3000);var server = io.listen(app); server.sockets.on('connection', function (client){ // new client is here! client.send ( 'Now connected!' ); client.on('message', function () { }) ; client.on('disconnect', function () { });});
client script:
<script src="http://localhost:3000/socket.io/socket.io.js"></script><script src="/assets/js/jquery-1.6.4.min.js"></script><script type="text/javascript"> $(document).ready(function(){ var socket = io.connect('http://localhost:3000'), text = $('#text'); socket.on('connect', function () { text.html('connected'); socket.on('message', function (msg) { text.html(msg); }); }); socket.on('disconnect', function () { text.html('disconnected'); }); });</script>
With everything up and running, upon going to my page, I see "Now connected!" almost immediately. I'm also serving my page up via CodeIgniter on MAMP - everything is working!