这是我在“HTML5 websocket 权威指南”一书中的代码。
....
<div id="output"></div>
<script>
function setup() {
output = document.getElementById("output");
ws = new WebSocket("ws://localhost:7777");
ws.onopen = function(e) {
log("Connected");
sendMessage("Hello Websocket!");
}
ws.onclose = function(e){
log("Disconnected: " + e.reason);
}
ws.onerror = function(e){
log("Error ");
}
ws.onmessage = function(e) {
log("Message received: " + e.data);
ws.close();
}
}
function sendMessage(msg){
ws.send(msg);
log("Message Sent");
}
function log(s){
var p = document.createElement("p");
p.style.wordWrap = "break-word";
p.textContent = s;
output.appendChild(p);
console.log(s);
}
setup();
</script>
Run Code Online (Sandbox Code Playgroud)
但是,当我在 localhost 中运行它时.. 输出就像这样
Connected
Message …Run Code Online (Sandbox Code Playgroud) 所以我正在考虑制作一个图形聊天应用程序/网站(用户坐在一个房间里,用 2D 头像聊天)并且从我迄今为止所做的所有研究(很多!)来看,LAMP 堆栈似乎适合我在网站的大部分时间里,使用一些 node.js 和 websocket 协议来处理发送的实际聊天数据(用户消息等)。我在创建这种范围内的任何东西方面都不是很有经验(我更习惯于前端工作),但到目前为止,这是一个有趣的挑战!
我的问题是,我似乎找不到很多有关验证用户信息的信息。我已经读过 websockets 本质上是相当不安全的,最好验证正在发送的数据的来源......
不过,我担心的是用户的实际身份。如果我使用 Javascript 来启动套接字连接,如何防止用户“欺骗”任何信息?是否可以将用户的 PHP 会话数据连接到处理 websocket 连接的服务器,以便 Javascript 除了实际的消息文本之外不会处理任何内容?
例如,如果我以“Kris”身份登录并发送消息“您好!” 或更改了我的头像的图像,我只希望客户端处理“你好”或头像图像 URL,并保留所有其他信息来回传递,例如服务器端的用户名,以防止任何人弄乱客户端代码。
我尝试在 Python 3.4 (Windows 7) 中使用 WebSockets 这是一个测试代码:
from twisted.internet import reactor
from autobahn.twisted.websocket import WebSocketClientFactory, WebSocketClientProtocol, connectWS
import json
class ClientProtocol(WebSocketClientProtocol):
def onConnect(self, response):
print("Server connected: {0}".format(response.peer))
def initMessage(self):
message_data = [{"type": "subscribe", "product_id": "BTC-USD"}]
message_json = json.dumps(message_data)
print("sendMessage: " + message_json)
self.sendMessage(message_json)
def onOpen(self):
print("onOpen calls initMessage()")
self.initMessage()
def onMessage(self, msg, binary):
print("Got echo: " + msg)
def onClose(self, wasClean, code, reason):
print("WebSocket connection closed: {0}".format(reason))
if __name__ == '__main__':
factory = WebSocketClientFactory("wss://ws-feed.exchange.coinbase.com")
factory.protocol = ClientProtocol
connectWS(factory)
reactor.run() …Run Code Online (Sandbox Code Playgroud) 我正在开发基于 websockets 和 webrtc 的聊天。我想向除发件人以外的所有连接用户发送消息,但我找不到合适的解决方案。更具体地说,我想向其他连接的用户发送新用户已加入聊天的通知。我试图为每个连接的用户提供一个唯一的 ID,但是每个新用户都会重写第一个分配的 ID,我无法区分用户。
服务器:
// list of users
var CLIENTS=[];
var id;
// web server is using 8081 port
var webSocketServer = new WebSocketServer.Server({ port: 8081 });
// check if connection is established
webSocketServer.on('connection', function(ws) {
id = Math.random();
CLIENTS[id] = ws;
CLIENTS.push(ws);
ws.on('message', function(message) {
console.log('received: %s', message);
var received = JSON.parse(message);
if(received.type == "login"){
ws.send(message); // send message to itself
/* *********************************************************** */
/* *** Here I trying to check if message comes from …Run Code Online (Sandbox Code Playgroud) 我的本地主机上有一个带有 ActionCable 的 Rails 5 应用程序,我正在尝试将它部署到 heroku。在访问聊天室所在的页面时,我可以在 chrome 的控制台中看到:
WebSocket connection to 'wss://full-beyond-9816.herokuapp.com/cable' failed: Error during WebSocket handshake: Unexpected response code: 404
Run Code Online (Sandbox Code Playgroud)
我确实在 heroku 上设置了 Redis 和 Redis 插件。这是我的 cable.yml 文件的生产部分:
production: &production
:url: redis://redistogo:4140ce7f3e7493bd1b12@porgy.redistogo.com:9463/
:host: tarpon.redistogo.com
:port: 10272
:password: 12e348ac10ca002879ce7d85daf0bb0
:inline: true
:timeout: 1
Run Code Online (Sandbox Code Playgroud)
这是我的 room.coffee 文件:
(function() {
this.App || (this.App = {});
App.cable = ActionCable.createConsumer();
}).call(this);
Run Code Online (Sandbox Code Playgroud)
在 heroku 上设置 ActionCable 似乎很棘手,我在这个主题上找到的每篇文章要么使用 Phusion Passenger(我使用的是 Puma),要么使用相当旧版本的 ActionCable(我使用的是 Rails 5 的最新测试版) )。
我应该如何设置?感谢您的时间和帮助
我想知道在两个用户之间发送消息的最佳方式是什么?我知道您可以创建房间并加入它们,但您必须先“创建”它们。把它想象成聊天信使。您只显示您在这两个用户之间收到的消息。
我可以做一个大对象,但最终那将是一个大对象。
你对处理这个问题有什么建议?
我对 c++ 还很陌生,我想知道如何将 web sockets 与它一起使用,我已经在 NodeJs 和 JavaScript 中使用了 web sockets,我想继续将它们与 c++ 一起使用。
我正在 Laravel 5.2 中做一个使用 websockets 的应用程序。对于 websocket 连接,我使用的是HoaServer,效果很好。
不好的部分是,我不知道如何将此服务器作为控制器,或者至少可以访问我的模型,现在我正在使用单独的 PDO 连接来进行数据库查询。
有人知道是否可以将此服务器作为控制器或至少可以通过 Laravel 模型访问数据库?
我现在的服务器:
require_once(__DIR__.'/../vendor/autoload.php');
$PDO = new PDO('mysql:host=127.0.0.1:3306;dbname=DBNAME', "USER", "PASS");
$websocket = new Hoa\Websocket\Server(new Hoa\Socket\Server('ws://'.$ip.':'.$porta));
$websocket->on('open', function (Hoa\Event\Bucket $bucket) {
return;
});
$websocket->on('message', function (Hoa\Event\Bucket $bucket) {
return;
});
$websocket->on('close', function (Hoa\Event\Bucket $bucket) {
return;
});
$websocket->run();
Run Code Online (Sandbox Code Playgroud)
我最喜欢的是触发laravel 事件,我不知道如何触发。:/
//Socket server message event
$server->on('message', function() {
//Fire your Laravel Event here
});
Run Code Online (Sandbox Code Playgroud) 我正在按照这里的说明进行操作:
https://testnet.bitmex.com/app/wsAPI
并且我已经确认以下Python实现有效(例如,从我的角度来看,没有网络问题),因为:
python wsdump.py \
wss://testnet.bitmex.com/realtime
> {"op":"subscribe","args":["orderBookL2_25:XBTUSD"]}
Run Code Online (Sandbox Code Playgroud)
结果是
{"success":true,"subscribe":"orderBookL2_25:XBTUSD","request":{"op":"subscribe","args":["orderBookL2_25:XBTUSD"]}}
Run Code Online (Sandbox Code Playgroud)
我试图修改大猩猩示例代码以将基本客户端组合在一起:
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/url"
"os"
"os/signal"
"time"
"github.com/gorilla/websocket"
)
var addr = flag.String("addr", "testnet.bitmex.com", "http service address")
func main() {
flag.Parse()
log.SetFlags(0)
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
u := url.URL{Scheme: "wss", Host: *addr, Path: "/realtime"}
log.Printf("connecting to %s", u.String())
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
log.Fatal("dial:", err)
}
defer c.Close()
done := make(chan struct{})
go func() …Run Code Online (Sandbox Code Playgroud) A Map.put() overwrites existing values in itself, despite putting in a unique value.
I am initializing a singleton of an object "Game" in which I have a map of Rooms (and Players).
When I input new key/value pair into the roomsList like this: roomsList.put(uniqueKey, new Room(uniqueKey, name)), the new key/value pair is added into the Map, but the rest of the pairs (each with a unique identifier) have their values overwritten as well.
I have tried putting in a new …