我在GoDaddy有网站托管。我是DigitalOcean的副总裁。
我去我的网站(在GoDaddy托管),并有一个输入字段(让它命名为贸易链接字段,因为它应该包含URL)。
用户将URL放在"贸易链接"字段>按"保存"按钮>数据正在通过Websocket发送到VPS(在DigitalOcean)>数据被保存在MongoDB。
最后一步不工作。我有一小段代码记录控制台的内容:
{"type":1,"steamid":"76561198092387621","link":"steamcommunity.com/tradeoffer/new/?partner=THISISURL";}
所以数据正在被服务器接收,这很好。现在有问题了。这些数据需要保存到MongoDB中。
让我们开始在我的网站(GoDaddy)输入字段:
<input type="text" rel="get-trade-link" id="par2" name="par2" placeholder="https://steamcommunity.com/tradeoffer/new/?partner=URL">
<div class="btn-yellow save-link">
然后script.js文件(Godaddy):
var socket = io.connect('http://url:8303');
ws = new WebSocket('ws://url:8305');
function sendTradeLink(tradelink){
ws.send(JSON.stringify({
type: 1,
steamid: getSteamID(),
link: tradelink
}));
}
$(document).on('click', '.save-link', function() {
var link = $('input[rel="get-trade-link"]').val();
if (link.indexOf('https://steamcommunity.com/tradeoffer/new/?partner=') < 0) {
} else {
sendTradeLink(link);
}
}
});
VPS:
var io = require('socket.io').listen(8303);
// all another
var MongoClient = require('mongodb').MongoClient;
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({ port: 8305 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
ws.send('something');
});
socket.on('1', function(data){
updateTradeLink(data.steamid, data.link);
});
'1' -我想它的意思是'类型'。这不是我的剧本,我买的,但是很难和他沟通。
socket.on('link', function(data){
userListDB.find({'steamid':data.steamId, 'type' : 'link'}).toArray(function(err, list) {
socket.emit('link', { list : list });
});
});
function updateTradeLink(sid, link) {
if(!g_Mongoconnected)
return 0;
existUser(sid, function(exist){
if(exist) {
userListDB.update({steamid: sid}, {steamid: sid, tradelink: link, type : 'link'});
} else {
userListDB.insert({steamid: sid, tradelink: link, type : 'link'}, {w:1}, function(err) {
if(err) {
helper.log('Error inserting tradelink', 485, err);
}
});
}
});
}
p。我买了那个脚本,并自己添加了那部分代码:
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({ port: 8305 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
ws.send('something');
});
编辑:MongoClient.connect('mongodb://localhost:27017', function (err, db) {
if (err) {
helper.log('Mongodb connection error', 425, err);
return 0;
}
userListDB = db.collection('users');
gameDB = db.collection('gamedb');
g_Mongoconnected = true;
helper.msg('mongo connected');
gameDB.find({name: 'counter'}).toArray(function (error, list) {
if(list.length == 0) {
currentGame=0;
gameDB.insert({name: 'counter', counter: 0}, {w:1}, function(err) {if(err) console.log('Error <1>');});
} else {
currentGame = list[0].counter;
}
});
});
试试这个修改:
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
if(message.type == 1){
updateTradeLink(message.steamid, message.link);
};
if(message.type =='link'){
userListDB.find({'steamid':message.steamId, 'type' : 'link'}).toArray(function(err, list) {
ws.emit('link', { list : list });
}
};
});
});
,然后到Mongo DB的东西:
function updateTradeLink(sid, link) {
if(!g_Mongoconnected){
console.log('Oops. Mongo DB not connected..');
return 0;
}
existUser(sid, function(exist){
if(exist) {
userListDB.update({steamid: sid}, {steamid: sid, tradelink: link, type : 'link'});
} else {
userListDB.insert({steamid: sid, tradelink: link, type : 'link'}, {w:1}, function(err) {
if(err) {
helper.log('Error inserting tradelink', 485, err);
}
});
}
});
}