-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6db1cce
commit e85e3c6
Showing
4 changed files
with
427 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>WebSocket</title> | ||
</head> | ||
<body> | ||
|
||
<a href="javascript:;" onclick="createFolder('http://www.baidu.com')">查看</a> | ||
<script type="text/javascript"> | ||
function createFolder (argument) { | ||
console.log(argument); | ||
} | ||
</script> | ||
|
||
|
||
<div id="output"></div> | ||
<script type="text/javascript" src="websocket.min.js"></script> | ||
<script> | ||
if (window.WebSocket){ | ||
log("此浏览器支持WebSocket的!"); | ||
} else { | ||
log("This browser does not support WebSocket."); | ||
} | ||
|
||
|
||
var socket = new ws('ws://127.0.0.1:3001'), | ||
str = "JSLite.io"; | ||
|
||
socket.onconnecting = function(evn){ | ||
console.log("socket:onconnecting:",evn); | ||
// sendMsg("wcj"); | ||
|
||
} | ||
socket.onopen = function(evn){ | ||
console.log("socket:onopen:",evn); | ||
log('发了个消息!"'+str+'"'); | ||
sendMsg(str); | ||
|
||
} | ||
socket.onclose = function(evn){ | ||
console.log("socket.onclose:",evn); | ||
log('WebSocket 被你关闭了!,您老人家再也没有办法建立连接了?'); | ||
|
||
} | ||
socket.onmessage = function(evn){ | ||
console.log("socket:onmessage:",evn); | ||
log('收到消息!"'+evn.data+'"'); | ||
// socket.close() | ||
|
||
} | ||
|
||
function sendMsg (str) { | ||
console.log("socket:sendMsg:",socket); | ||
socket.send(str); | ||
} | ||
|
||
|
||
///======================================== | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
// function setup(){ | ||
// var wsServer = 'ws://127.0.0.1:3001'; | ||
// var ws = new WebSocket(wsServer); | ||
|
||
// ws.onopen = function (e) { | ||
// log("Connected to WebSocket server.",e); | ||
// sendMessage("Conan"); | ||
// } | ||
|
||
// ws.onclose = function (e) { | ||
// log("Disconnected",e); | ||
// } | ||
|
||
// ws.onmessage = function(e) { | ||
// log("RECEIVED: " + e.data, e); | ||
// ws.close(); | ||
// } | ||
|
||
// ws.onerror = function (e) { | ||
// log('Error occured: ' + e.data,e); | ||
// } | ||
|
||
// var sendMessage = function(msg){ | ||
// ws.send(msg); | ||
// log("SEND : "+ msg); | ||
// } | ||
// } | ||
// setup(); | ||
|
||
function log(s,e) { | ||
var output = document.getElementById("output"), | ||
p = document.createElement("p"); | ||
|
||
p.style.wordWrap = "break-word"; | ||
p.style.padding="10px"; | ||
p.style.background="#eee"; | ||
p.textContent = "LOG : "+s; | ||
output.appendChild(p); | ||
// console.log("LOG : "+s, e); | ||
} | ||
|
||
</script> | ||
|
||
|
||
</body> | ||
</html> | ||
|
||
<!-- | ||
<html> | ||
<body> | ||
<div id="output"></div> | ||
<script type="text/javascript"></script> | ||
<script> | ||
function checkBrowser(){ | ||
if (window.WebSocket){ | ||
log("此浏览器支持WebSocket的!"); | ||
} else { | ||
log("This browser does not support WebSocket."); | ||
} | ||
} | ||
function setup(){ | ||
var wsServer = 'ws://127.0.0.1:3001'; | ||
var ws = new WebSocket(wsServer); | ||
ws.onopen = function (e) { | ||
log("Connected to WebSocket server.",e); | ||
sendMessage("Conan"); | ||
} | ||
ws.onclose = function (e) { | ||
log("Disconnected",e); | ||
} | ||
ws.onmessage = function(e) { | ||
log("RECEIVED: " + e.data, e); | ||
ws.close(); | ||
} | ||
ws.onerror = function (e) { | ||
log('Error occured: ' + e.data,e); | ||
} | ||
var sendMessage = function(msg){ | ||
ws.send(msg); | ||
log("SEND : "+ msg); | ||
} | ||
} | ||
checkBrowser(); | ||
setup(); | ||
function log(s,e) { | ||
var output = document.getElementById("output"), | ||
p = document.createElement("p"); | ||
p.style.wordWrap = "break-word"; | ||
p.style.padding="10px"; | ||
p.style.background="#eee"; | ||
p.textContent = "LOG : "+s; | ||
output.appendChild(p); | ||
console.log("LOG : "+s, e); | ||
} | ||
</script> | ||
</body> | ||
</html> --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
var ws = require("nodejs-websocket"); | ||
var server = ws.createServer(function(conn){ | ||
conn.on("text", function (str) { | ||
console.log("收到的信息为:"+str) | ||
conn.sendText(str) | ||
}) | ||
conn.on("close", function (code, reason) { | ||
console.log("关闭连接") | ||
}); | ||
conn.on("error", function (code, reason) { | ||
console.log("异常关闭") | ||
}); | ||
}).listen(3001) | ||
|
||
|
||
console.log("--WebSocket-------------") | ||
console.log("WebSocket address: ws://127.0.0.1:3001") | ||
console.log("WebSocket has started.") | ||
console.log("------------------------") | ||
|
||
|
||
var http = require("http"), | ||
url = require("url"), | ||
path = require("path"), | ||
fs = require("fs"); | ||
|
||
http.createServer(function (req, res) { | ||
var pathname=__dirname+url.parse(req.url).pathname; | ||
if (path.extname(pathname)=="") { | ||
pathname+="/"; | ||
} | ||
if (pathname.charAt(pathname.length-1)=="/"){ | ||
pathname+="index.html"; | ||
} | ||
|
||
path.exists(pathname,function(exists){ | ||
if(exists){ | ||
switch(path.extname(pathname)){ | ||
case ".html": | ||
res.writeHead(200, {"Content-Type": "text/html"}); | ||
break; | ||
case ".js": | ||
res.writeHead(200, {"Content-Type": "text/javascript"}); | ||
break; | ||
case ".css": | ||
res.writeHead(200, {"Content-Type": "text/css"}); | ||
break; | ||
case ".gif": | ||
res.writeHead(200, {"Content-Type": "image/gif"}); | ||
break; | ||
case ".jpg": | ||
res.writeHead(200, {"Content-Type": "image/jpeg"}); | ||
break; | ||
case ".png": | ||
res.writeHead(200, {"Content-Type": "image/png"}); | ||
break; | ||
default: | ||
res.writeHead(200, {"Content-Type": "application/octet-stream"}); | ||
} | ||
|
||
fs.readFile(pathname,function (err,data){ | ||
res.end(data); | ||
}); | ||
} else { | ||
res.writeHead(404, {"Content-Type": "text/html"}); | ||
res.end("<h1>404 Not Found</h1>"); | ||
} | ||
}); | ||
|
||
}).listen(8080, "127.0.0.1"); | ||
|
||
|
||
console.log("--Server----------------") | ||
console.log("Server address: http://127.0.0.1:8080") | ||
console.log("Server running... press ctrl-c to stop.") | ||
console.log("Server has started.") | ||
console.log("------------------------") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
var ws = require("nodejs-websocket"); | ||
var server = ws.createServer(function(conn){ | ||
conn.on("text", function (str) { | ||
console.log("收到的信息为:"+str) | ||
conn.sendText(str) | ||
}) | ||
conn.on("close", function (code, reason) { | ||
console.log("关闭连接") | ||
}); | ||
conn.on("error", function (code, reason) { | ||
console.log("异常关闭") | ||
}); | ||
}).listen(3002) | ||
|
||
console.log("--WebSocket-------------") | ||
console.log("WebSocket address: ws://127.0.0.1:3002") | ||
console.log("WebSocket has started.") | ||
console.log("------------------------") |
Oops, something went wrong.