Javascript websocket客户端库,可以在握手请求中传递自定义标头
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Javascript websocket客户端库,可以在握手请求中传递自定义标头,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3094字,纯文字阅读大概需要5分钟。
内容图文
我需要一个javascript库连接到我的web-socket服务器,这是使用python twisted实现的.我尝试了原生javascript web-socket客户端,但它没有传递自定义头 as per this link的选项.我的web-socket服务器通过从Oauth2标准中的握手头中获取auth_token来进行身份验证.是否有任何javascript库可用于Web套接字客户端,允许在连接时传递自定义标头?
解决方法:
我很遗憾成为坏消息的承载者…但是 – 正如the question中所提到的那样,你正在引用,你可以从the standard Websocket API学到(这不是一个外部库,它是浏览器附带的)…您无法为websocket连接设置自定义标头.
The WebSocket(url, protocols) constructor takes one or two arguments. The first argument, url, specifies the URL to which to connect. The second, protocols, if present, is either a string or an array of strings. … Each string in the array is a subprotocol name. The connection will only be established if the server reports that it has selected one of these subprotocols. …
但是,一切都不会丢失.
由于这是您的websocket服务器,您可以选择:
>我非常确定OAuth2使用令牌作为GET或POST请求的参数,而不是自定义标头.这意味着(也许)您可以将令牌作为连接字符串的一部分传递,即:
websocket = new WebSocket('wss://my.server.com/?access_token=secret_acess_token');
像这样传递会话令牌可能并不理想,可能会带来安全风险……所以我会选择第二个选项:
>新的websocket连接(除非我的浏览器是特殊的)使用与建立主连接相同的cookie启动 – 这意味着来自Http层的所有cookie和会话数据都可以被websocket层访问….
因此,可以设置一个唯一的cookie – 或者甚至更好(假设您的http和websocket共享相同的代码库并一起工作),在服务器端会话存储中设置身份验证令牌 – 并使用该数据来验证连接或者拒绝它.
由于我不是Python专家,这里是使用Ruby的Plezi framework(我是作者)的快速演示:
require 'plezi'
class DemoCtrl
# this is the Http index page response
def index
response.write "#{cookies[:notice]}\n\n" if cookies[:notice] && (cookies[:notice] = nil).nil?
#returning a string automatically appends it to the response.
"We have cookies where we can place data:\n#{request.cookies.to_s}\n"
end
# the login page
def login
cookies[:my_token] = "a secret token"
cookies[:notice] = "logged in"
redirect_to :index
end
# the logout page
def logout
cookies[:my_token] = nil
cookies[:notice] = "logged out"
redirect_to :index
end
# a Plezi callback, called before a websocket connection is accepted.
# it's great place for authentication.
def pre_connect
puts "Websocket connections gave us cookies where we can place data:\n#{request.cookies.to_s}\n"
return false unless cookies.to_s[:my_token] == "a secret token"
# returning true allows the connection to be established
true
end
def on_message data
puts "echoing #{data}"
response << "echo: #{data}"
end
end
# setup the route to our demo
Plezi.route '/', DemoCtrl
# Plezi will start once the script is finished.
# if you are running this in irb, use:
exit
尝试启动websocket,打开Web检查器并在控制台中运行以下脚本:
ws = new WebSocket("ws://localhost:3000/"); ws.onopen = function(e) { console.log("open"); }; ws.onmessage = function(e) { console.log(e.data);};
ws.send("Go Bears");
这应该失败,因为我们还没有验证……
访问http://loaclhost:3000/login并再试一次.
现在它应该工作.
如果您愿意,请尝试http://loaclhost:3000/logout.
内容总结
以上是互联网集市为您收集整理的Javascript websocket客户端库,可以在握手请求中传递自定义标头全部内容,希望文章能够帮你解决Javascript websocket客户端库,可以在握手请求中传递自定义标头所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。