Skip to content

Commit

Permalink
调整分享功能 
Browse files Browse the repository at this point in the history
  • Loading branch information
ggaaooppeenngg committed Feb 23, 2015
1 parent af9311a commit 7ef038c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 19 deletions.
6 changes: 6 additions & 0 deletions models_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ func (c *Code) Save(db *mgo.Database) error {
return cln.Insert(c)
}

// 更新代码
func (c *Code) Update(db *mgo.Database, update bson.M) error {
cln := db.C(CODE)
return cln.UpdateId(c.Id_, bson.M{"$set": update})
}

// 通过Id获取代码
func GetCodeById(hex string, db *mgo.Database) (*Code, error) {
cln := db.C(CODE)
Expand Down
30 changes: 27 additions & 3 deletions playground.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import (
type CMD string

const (
RUN CMD = "run"
CLOSE CMD = "close"
SHARE CMD = "share"
RUN CMD = "run"
CLOSE CMD = "close"
SHARE CMD = "share"
UPDATE CMD = "update"
)

type Command struct {
Expand Down Expand Up @@ -104,6 +105,29 @@ func playWebSocket(handler *Handler) func(ws *websocket.Conn) {
if cmd.Command == CLOSE {
break
}
if cmd.Command == UPDATE {
println("update")
var id = bson.ObjectIdHex(cmd.Id)

code := &Code{
Id_: id,
}
err := code.Update(handler.DB, bson.M{
"content": cmd.Content,
})
if err != nil {
logger.Println(err)
websocket.JSON.Send(ws, Res{
Command: SHARE,
Err: err.Error(),
})
break
}
websocket.JSON.Send(ws, Res{
Command: SHARE,
Content: id.Hex(),
})
}

if cmd.Command == SHARE {
id := bson.NewObjectId()
Expand Down
46 changes: 30 additions & 16 deletions templates/playground.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,78 +26,92 @@
// console 重定向
(function(){
var oldLog = console.log;

console.log = function(message){
$("#response").css("color","black");
$("#response").text(message);
var output = $("#response").html();
var message = message.replace(/\n/g,"<br>");
$("#response").html(output+message);
oldLog.apply(console,arguments);
};
})();

function setURL(id){
console.log(id);
var url = "{{.Host}}"+"/play/"+id;
$("#shareURL").val(url);
$("#shareURL").select();
};

var socket = new WebSocket("ws://localhost:8888/playsocket");
socket.onopen = function(event){

// 分享代码
$( "#share" ).click(function(){
$("#shareURL").css("display","inline-block");
var id = "{{.CodeId}}";
if(id!==""){
var url = "{{.Host}}"+"/play/"+id;
$("#shareURL").val(url);
$("#shareURL").select();
// 更新代码
alert("update");
var cmd = {
Command:"update",
Id:id,
Content:$("#code").val(),
}
socket.send(JSON.stringify(cmd));
}else{
alert("share");
// 提交新的代码.
var code = $("#code").text();
var cmd = {
Command:"share",
Content:code,
Content:$("#code").val(),
}
socket.send(JSON.stringify(cmd));
}
});

// 运行代码
$( "#run" ).click(function(){
var code = $("#code").val();
var cmd = {
Command:"run",
Content:code,
Content:$("#code").val(),
}
$("#response").text("excuting...");
socket.send(JSON.stringify(cmd));
});

window.onunload = function(){
var cmd = {
Command:"close",
}
socket.send(JSON.stringify(cmd));
};
}

socket.onmessage = function(event){
var data = JSON.parse(event.data);

if(data.Command == "run"){
if(data.Err){
var err = data.Err;
$("#response").css("color","red");
$("#response").text(err);
//alert(err);
}else{
$("#response").text("");
var ret = data.Content;
eval(ret);
}
}

if(data.Command == "share"){
if(data.Command == "share" ||
data.Command == "update"){
if(data.Err){
//alert("存在错误:\n"+data.Err);
$("#response").css("color","red");
$("#response").text("存在错误:\n"+data.Err);
}else{
$("#shareURL").css("display","inline-block");
var url = "{{.Host}}"+"/play/"+data.Content;
$("#shareURL").val(url);
$("#shareURL").select();
}
}

}
</script>
</html>

0 comments on commit 7ef038c

Please sign in to comment.