开发者

Where should i write the client side code for socket.io?

开发者 https://www.devze.com 2023-03-21 06:30 出处:网络
I am a beginner to node.js and more so , to socket.io. In node.js, there is no need for any client side code as far as i know. We can simply connect to the server by pointing the browser to http://loc

I am a beginner to node.js and more so , to socket.io. In node.js, there is no need for any client side code as far as i know. We can simply connect to the server by pointing the browser to http://localhost:port.

But, in the case of socket.io, there seems to be client side code. But, w开发者_如何学运维here should i write this ? Should i write this in a html file and host it from a normal http server? I am a bit confused about the basic architecture itself and i couldnt find this basic explanation anywhere. Please do bear with me

Thanks


I recently wrote a very simple chat application with Node.js and Socket.IO. Like the above comment, you can use express to server your HTML by configuring a static directory:

app.configure(function() {
  app.use(express.static(__dirname + '/'));
});

See the full code here: https://github.com/pifantastic/stupid-simple-chat/blob/master/server.js

*EDIT*

I should add, in Socket.IO > 0.7, you can serve the socket.io JS library straight out of Node.js, it lives at /socket.io/socket.io.js:

<script src="/socket.io/socket.io.js"></script>


First if you're not using express you probably should be. Among other things it's a static file server so you can put your client side JavaScripts into /public/javascripts and use them from http://localhost:port/javascripts/myscript.js

Here's a hello world example http://codr.cc/s/2376dc51/js

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号