Steam for Node.js
This is a fork of node-steam's SteamClient. Essentially it's node-steam without the handler modules and with some more features. It should be compatible with all node-steam handler modules, as long as the underlying Steam protocol doesn't change without a third-party module's knowledge.
This exists because of how painfully slow it is to get new things implemented into node-steam, and also because of incompatibilities that can potentially arise between node-steam and third-party handler modules.
Protocol version bumps will always be major module releases. You're highly recommended to pin the major version in your
dependencies (e.g. "steam-client": "^1.0.0"
).
Requires Node.js v4.1.1 or later.
Installation
$ npm install steam-client
Usage
First, require
this module.
var Steam = ;
Steam
is now a namespace object containing:
Then you'll want to create an instance of CMClient and any handlers you need (externally), call
SteamClient#connect and assign event listeners. Some handlers, such as
node-steam-user
do this automatically for you.
var steamClient = ;steamClient;steamClient; steamClient;
Constructor
The constructor takes one argument: the protocol to use to connect to Steam. This should be a value from
EConnectionProtocol
.
Default is TCP. UDP support is experimental. There are some pros and cons to each:
- TCP
- Pro: Operating system manages the connection, so you will automatically disconnect if your app crashes or is killed
- Con: Less fine control over the connection. It's up to the OS to detect poor network conditions and kill the connection
- UDP
- Pro: Finer control over the connection. Able to tear down a broken connection faster in cases where the OS wouldn't detect it
- Pro: Gives you access to the server load of the CM you connected to
- Con: If your app crashes or is killed without properly logging off or disconnecting, your session will remain active for a minute while Steam waits for it to timeout
- Con: Currently support for UDP connections in
CMClient
is experimental
- WebSocket
- Pro: More secure crypto. Uses TLSv1.2 instead of Valve's proprietary crypto, which has been broken (and fixed) in the past
- Pro: Runs over TCP, so all the benefits of TCP
- Pro:
CMClient
's implementation of Valve's WebSocket protocol sends ping frames to the CM every 30 seconds, which the CM responds to, allowing for faster detection of dropped connections - Pro: Runs over port 443, allowing access through nearly all firewalls
- Con: Currently support for WebSocket connections in
CMClient
is in beta
Note that UDP connections use Valve-brand UDP, which is essentially TCP over UDP. Consequently, network unreliability is not a concern when using UDP.
Example:
var Steam = ;var client = SteamEConnectionProtocolTCP;
Servers
Steam.servers
contains the list of CM servers that CMClient
will attempt to connect to. The bootstrapped list is not
always up-to-date and might contain dead servers. To avoid timeouts, replace it with your own list before logging in if
you have one (see 'servers' event).
SteamID
Since JavaScript's Number type does not have enough precision to store 64-bit integers, SteamIDs are represented as decimal strings. (Just wrap the number in quotes)
Enums
Whenever a method accepts (or an event provides) an ESomething
, it's a Number that represents some enum value. See
enums.steamd and
eresult.steamd for the whole
list of them. For each enum, there is an equivalently named property on Steam
. The property is an object; for each of
the enum's members, there is an equivalently named property on the object with an equivalent value.
Note that you can't easily get the string value from the number, but you probably don't need to. You can still use them
in conditions (e.g. if (type == Steam.EChatEntryType.Emote) ...
) or switch statements.
Protobufs
Whenever a method accepts (or an event provides) a CMsgSomething
, it's an object that represents a protobuf message.
It has an equivalently named property for each set field in the specified message with the type as follows:
(u)int32
andfixed32
fields: Numberuint64
,fixed64
andstring
fields: Stringbytes
fields: Buffer objectsbool
fields: Boolean
See the node-steam wiki for descriptions of protobuf fields.
Handlers
Most of the API is provided by handler classes that internally send and receive low-level client messages using 'message'/send.
This module has no handlers built-in. You may use handlers from node-steam
, or
you may alternatively use standalone handlers (such as node-steam-user
).
CMClient
Properties
connected
A boolean that indicates whether you are currently connected and the encryption handshake is complete.
'connected' is emitted when it changes to true
, and 'error' is emitted when it changes to
false
unless you called disconnect. Sending any client messages is only allowed while this is true
.
loggedOn
A boolean that indicates whether you are currently logged on. Calling any handler methods except for methods to log on is only allowed while logged on.
steamID
Your own SteamID while logged on, otherwise unspecified. Must be set to a valid initial value before sending a logon message.
remoteAddress
v2.1.0 or later is required to use this property
If we've initiated a connection previously, a string containing "ipv4:port" for the server we're connecting/connected to. Also contains the address of the last host we were connected to if we're currently disconnected.
Methods
bind([localAddress][, localPort])
localAddress
- The local IP address you want to use for the outgoing connectionlocalPort
- The local port you want to use for the outgoing connection
Override the address and/or port that will be used for the outgoing connection. Takes effect the next time you connect.
setHttpProxy(proxyUrl)
proxyUrl
- A string containing the URL of your HTTP proxy
If you want to connect via an HTTP proxy, set the proxy URL using this method before connecting. HTTPS proxies are not
supported at this time. Example: http://user:pass@1.2.3.4:8080
connect([server][, autoRetry])
server
- If you want to connect to a specific CM server, provide an object here containinghost
andport
properties. Default is a random value from theservers
property.autoRetry
-true
if you want to automatically retry connection until successful, orfalse
if you want anerror
event if connection fails. Defaulttrue
.
Connects to Steam. It will keep trying to reconnect (provided autoRetry
is not false
) until encryption handshake is
complete (see 'connected'), unless you cancel it with disconnect.
You can call this method at any time. If you are already connected, disconnects you first. If there is an ongoing connection attempt, cancels it.
disconnect()
Immediately terminates the connection and prevents any events (including 'error') from being emitted until you connect again. If you are already disconnected, does nothing. If there is an ongoing connection attempt, cancels it.
logOn(details)
details
- An object containing your logon parameters
Send a logon message to the CM. You must first be connected and set steamID
to a valid initial value.
You will receive the response in the logOnResponse
event.
send(header, body, callback)
header
- An object containing the message header. It has the following properties:msg
- A value fromEMsg
proto
- ACMsgProtoBufHeader
object if this message is protobuf-backed, otherwiseheader.proto
is falsy. The following fields are reserved for internal use and shall be ignored:steamid
,client_sessionid
,jobid_source
,jobid_target
. (Note: pass an empty object if you don't need to set any fields)
body
- ABuffer
orByteBuffer
containing the rest of the messagecallback
(optional) - if not falsy, then this message is a request, andcallback
shall be called with any response to it instead of 'message'/send.callback
has the same arguments as 'message'/send.
Events
error
err
- AnError
object. May contain aneresult
property.
Connection closed by the server. Only emitted if the encryption handshake is complete, otherwise it will reconnect
automatically (unless you disabled autoRetry
). loggedOn
is now false
.
connected
serverLoad
- The load value of the CM server you're connected to. Only available if you're connecting using UDP. It's unclear at this time what scale this value uses.
Encryption handshake complete. From now on, it's your responsibility to handle disconnections and reconnect
(see error
). You'll likely want to log on now.
logOnResponse
response
- An object with the properties inCMsgClientLogonResponse
Logon response received. If eresult
is EResult.OK
, loggedOn
is now true
.
servers
servers
- An array containing the up-to-date server list
CMClient will use this new list when reconnecting, but it will be lost when your application restarts. You might want
to save it to a file or a database and assign it to Steam.servers
before logging in next time.
Note that Steam.servers
will be automatically updated after this event is emitted. This will be useful if you want
to compare the old list with the new one for some reason - otherwise it shouldn't matter.
loggedOff
eresult
- A value fromEResult
You were logged off from Steam. loggedOn
is now false
.
message
header
- An object containing the message headerbody
- ABuffer
containing the rest of the messagecallback
- If set, then this message is a request and Steam expects a response back from you. To respond, call this callback instead of usingsend()
.
Emitted when you receive a message from the CM.