You have to use tooltip scanning. Here's the code I use to find the number of sockets a given item has, with some added comments to explain what's going on:
Code:
local GetNumSockets
do
-- Generate a unique name for the tooltip:
local tooltipName = "PhanxScanningTooltip" .. random(100000, 10000000)
-- Create the hidden tooltip object:
local tooltip = CreateFrame("GameTooltip", tooltipName, UIParent, "GameTooltipTemplate")
tooltip:SetOwner(UIParent, "ANCHOR_NONE")
-- Build a list of the tooltip's texture objects:
local textures = {}
for i = 1, 10 do
textures[i] = _G[tooltipName .. "Texture" .. i]
end
-- Set up scanning and caching:
local numSocketsFromLink = setmetatable({}, { __index = function(t, link)
-- Send the link to the tooltip:
tooltip:SetHyperlink(link)
-- Count how many textures are shown:
local n = 0
for i = 1, 10 do
if textures[i]:IsShown() then
n = n + 1
end
end
-- Cache and return the count for this link:
t[link] = n
return n
end })
-- Expose the API:
function GetNumSockets(link)
return link and numSocketsFromLink[link]
end
end
Then you can just call GetNumSockets with the item link. It does add a little complexity to maintain a cache, so it'll only ever actually do the scanning once per item link, and if you call it with the same link again, it'll just give you the cached value.
It
does not handle cases where the item isn't already in your local item cache, though -- in that case you'll need to wait and call it again once your client receives the item data.
And, though you didn't ask, here's how to count how many gems are
currently on the item:
Code:
local _, itemID, enchantID, gem1, gem2, gem3, gem4 = strsplit(":", strmatch(link, "|H(.-)|h"))
local numFilledSockets = (tonumber(gem1) or 0) + (tonumber(gem2) or 0) + (tonumber(gem3) or 0) + (tonumber(gem4) or 0)