var http = require('http');
server = http.createServer(function (req, res) {
res.writeHeader(200, {"Content-Type": "text/plain"});
res.end("Hello oschina\n");
})
server.listen(8000);
console.log("httpd start @8000");
环境应该差不多了。下面要开始用node.js写socketServer了
在谷歌里搜索了很多资料,本想基于一位老外写的模块进行测试,发现跑不起来。
他的项目:Basic-Node.js-Websocket-Chat
启动的时候报找不到utils模块,折腾了半天,也没能跑起来,果断放弃了,继续寻找…
后来找到这篇文章:Node.js and HTML5 Web Sockets,在里面找到别人写好的模块:node.ws.js
它的主页有例子,告诉使用者如何使用它的模块。
var sys = require("sys"),
ws = require("./ws");
ws.createServer(function (websocket) {
websocket.addListener("connect", function (resource) {
// emitted after handshake
sys.debug("connect: " + resource);
// server closes connection after 10s, will also get "close" event
setTimeout(websocket.end, 10 * 1000);
}).addListener("data", function (data) {
// handle incoming data
sys.debug(data);
// send data to client
websocket.write("Thanks!");
}).addListener("close", function () {
// emitted when server or client closes connection
sys.debug("close");
});
}).listen(8080);
我下载了ws.js,然后将它放在node.js解压目录下的lib目录中D:\QMDownload\nodejs-0.4.6\lib\ws.js
然后基于这个模块写socket server(socket.js--存放于D:\QMDownload\nodejs-0.4.6目录下):
var sys = require("sys"),
ws = require("../lib/ws");
<script src="jquery-1.3.2.min.js" type="text/javascript"></script> 1: 2: <script type="text/javascript"> 3: var ws; 4: $(document).ready(function () { 5: 6: if ("WebSocket" in window) { 7: debug("Browser supports web sockets!", 'success'); 8: connect($('#host').val()); 9: $('#console_send').removeAttr('disabled'); 10: } else { 11: debug("Browser does not support web sockets", 'error'); 12: }; 13: 14: // function to send data on the web socket 15: function ws_send(str) { 16: try { 17: ws.send(str); 18: } catch (err) { 19: debug(err, 'error'); 20: } 21: } 22: 23: // connect to the specified host 24: function connect(host) { 25: 26: debug("Connecting to " + host + " ..."); 27: try { 28: ws = new WebSocket(host); // create the web socket 29: } catch (err) { 30: debug(err, 'error'); 31: } 32: $('#host_connect').attr('disabled', true); // disable the 'reconnect' button 33: 34: ws.onopen = function () { 35: debug("connected... ", 'success'); // we are in! :D 36: }; 37: 38: ws.onmessage = function (evt) { 39: debug(evt.data, 'response'); // we got some data - show it omg!! 40: }; 41: 42: ws.onclose = function () { 43: debug("Socket closed!", 'error'); // the socket was closed (this could be an error or simply that there is no server) 44: $('#host_connect').attr('disabled', false); // re-enable the 'reconnect button 45: }; 46: }; 47: 48: // function to display stuff, the second parameter is the class of the <p> (used for styling) 49: function debug(msg, type) { 50: $("#console").append('<p class="' + (type || '') + '">' + msg + '</p>'); 51: }; 52: 53: // the user clicked to 'reconnect' button 54: $('#host_connect').click(function () { 55: debug("\n"); 56: connect($('#host').val()); 57: }); 58: 59: // the user clicked the send button 60: $('#console_send').click(function () { 61: ws_send($('#console_input').val()); 62: }); 63: 64: $('#console_input').keyup(function (e) { 65: if(e.keyCode == 13) // enter is pressed 66: ws_send($('#console_input').val()); 67: }); 68: 69: });</script>